Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Database and Reporting (http://www.xtremevbtalk.com/-net-database-and-reporting/)
-   -   variable loses its value once exiting the if statement (http://www.xtremevbtalk.com/-net-database-and-reporting/328235-variable-loses-value-exiting-statement.html)

Maany 05-26-2016 08:31 AM

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


PlausiblyDamp 05-26-2016 09:13 AM

Your code assumes
Code:

If Not Rs.EOF Then
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.

Maany 05-26-2016 10:31 AM

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 !

TheRealTinTin 05-26-2016 12:09 PM

Is this legacy VB or VB.Net? It sounds like a .Net message.

Try using
Code:

Return C_Code
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. :confused:

Cerian Knight 05-26-2016 01:08 PM

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.

Kluz 07-08-2017 11:11 PM

I'm not .net savvy, but why not:
Code:
Get_Code = Rs.Fields("Code").Value
Why the intermediate, use-once variable?


All times are GMT -6. The time now is 03:55 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.