 |

05-26-2016, 07:31 AM
|
Newcomer
|
|
Join Date: Nov 2014
Posts: 9
|
|
variable loses its value once exiting the if statement
hi all,
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".
why ?!?!?
Code:
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
End If
Rs.Close()
CN.Close()
Get_Code = C_Code
End Function
|
|

05-26-2016, 08:13 AM
|
 |
Ultimate Contributor
Forum Leader * Expert *
|
|
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
|
|
Your code assumes
will never be false, if the line of code
Code:
Rs.Open("Select * from Countries where Country like '" & Country_Name & "'", CN)
didn't have any matches the variable C_Code would never be assigned a value.
|
|

05-26-2016, 09:31 AM
|
Newcomer
|
|
Join Date: Nov 2014
Posts: 9
|
|
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 !
|
|

05-26-2016, 11:09 AM
|
 |
Junior Contributor
|
|
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 383
|
|
Is this legacy VB or VB.Net? It sounds like a .Net message.
Try using The reason you're getting the warning message is because the variable has not been assigned a value when it was created. Try declaring the variable like this
Code:
Dim C_Code As String = Nothing
Also, is the recordset definitely returning records? I don't claim to be an expert but the SQL syntax doesn't look like it would work. 
|
__________________
Artificial Intelligence is no match for natural stupidity
|

05-26-2016, 12:08 PM
|
 |
Polymath (in disciplina)
Super Moderator * Expert *
|
|
Join Date: May 2004
Location: Michigan
Posts: 4,206
|
|
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').
|

07-08-2017, 10:11 PM
|
 |
Sapience.Aquire
Super Moderator * Expert *
|
|
Join Date: Oct 2003
Location: Lake Bluff, Ill., U.S.
Posts: 3,459
|
|
I'm not .net savvy, but why not:
Code:
Get_Code = Rs.Fields("Code").Value
Why the intermediate, use-once variable?
|
__________________
No the other right mouse click
|
Tags
|
c_code, string, variable, country_name, function, public, get_codebyref, reference, null, exception, runtime, result, countries, rs.eof, rs.fieldscode.value, rs.close, get_code, cn.close, rs.openselect, dim, assigned, country, loses, driving, code  |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|