variable problem

Bill Humphrey

Active member
Joined
Apr 10, 2008
Messages
35
Programming Experience
Beginner
I've just migrated a project from VS 2003 to 2005 and this code is giving me an error:
VB.NET:
  Try
                Dim oLog As Object

            If [COLOR="SeaGreen"]oLog [/COLOR]Is Nothing Then
                oLog = New EventLog("CC", System.Environment.MachineName, "CC Application  Log")
            End If
the error is: variable Olog has been used before it has been assigned a value, a null reference exception could result at runtime.

Regards BH
 
If you can't assign the variable an instance right away you can set it to Nothing explicitly to remove that warning, telling the compiler that you know what you're doing...
 
Hi,

I tried setting oLog = "" in visual studio 2008 and it is not giving any warning. As JohnH said, set it to Nothing instead of "" which is an empty string


VB.NET:
 oLog = Nothing
 
Dim oLog As Object

oLog = ""
This is not "right away", this is declaring the variable, and some other time assigning it. Assign the variable a value in the same line you declare it.
VB.NET:
Dim oLog As Object = Nothing
 
Back
Top