variable loses its value once exiting the if statement
this may sound stupid and silly but it is driving me crazy, in below code C_Code= maintain its value returned from the sql database till the line in red below, it becomes "", and it gives a warning saying :"Variable 'C_Code' is used before it has been assigned a value, a null reference exception could result at runtime".
Public Function Get_Code(ByRef Country_Name As String) As String
Dim C_Code As String
Rs.Open("Select * from Countries where Country like '" & Country_Name & "'", CN)
If Not Rs.EOF Then
C_Code = Rs.Fields("Code").Value
Get_Code = C_Code
Thank you for your prompt response, records are eof is always false, because records always exist, and using breakpoints i can see that it was actually assigned a value, but magically when it reaches that line in red, its value magically becomes null !
It is a .Net warning and I agree that assigning C_Code a default value when declared is best practice (and will suppress the warning because the existing assignment is only in a conditional block, which the compiler is aware of).
As to why the value changes, just to be sure, confirm that there are no scope issues with the same variable/function names occurring elsewhere in the application.
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').