variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement
variable loses its value once exiting the if statement variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
Go Back  Xtreme Visual Basic Talk > > > variable loses its value once exiting the if statement


Reply
 
Thread Tools Display Modes
  #1  
Old 05-26-2016, 08:31 AM
Maany Maany is offline
Newcomer
 
Join Date: Nov 2014
Posts: 9
Angry 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
Reply With Quote
  #2  
Old 05-26-2016, 09:13 AM
PlausiblyDamp's Avatar
PlausiblyDampvariable loses its value once exiting the if statement PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,051
Default

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.
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 05-26-2016, 10:31 AM
Maany Maany is offline
Newcomer
 
Join Date: Nov 2014
Posts: 9
Default

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 !
Reply With Quote
  #4  
Old 05-26-2016, 12:09 PM
TheRealTinTin's Avatar
TheRealTinTin TheRealTinTin is offline
Junior Contributor
 
Join Date: Nov 2008
Location: Glasgow, UK
Posts: 375
Default

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.
__________________
Artificial Intelligence is no match for natural stupidity
Reply With Quote
  #5  
Old 05-26-2016, 01:08 PM
Cerian Knight's Avatar
Cerian Knightvariable loses its value once exiting the if statement Cerian Knight is offline
Multi-Technologist

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,143
Default

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').
Reply With Quote
Reply

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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
 
variable loses its value once exiting the if statement
variable loses its value once exiting the if statement
 
-->