IsNothing and null reference exceptions

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I have several places in my code in which I use IsNothing(myobject) to test
whether a variable is Nothing. Even though my code compiles and works with
no problems, Visual Studio 2008 is giving me the following warning:

Variable 'ddlYear' is used before it has been assigned a value. A null
reference exception could result at runtime.

Even though it is true that ddlyear has not been assigned a value when I use
IsNothing, isn't that the whole point of using IsNothing? Why does Visual
Studio 2008 give this warning for objects when they are inside of IsNothing?
This is not a high priority or anything I will make a big deal out of, just
wondering if anybody has any comments or suggestions.
 
G

Göran Andersson

Nathan said:
I have several places in my code in which I use IsNothing(myobject) to test
whether a variable is Nothing. Even though my code compiles and works with
no problems, Visual Studio 2008 is giving me the following warning:

Variable 'ddlYear' is used before it has been assigned a value. A null
reference exception could result at runtime.

Even though it is true that ddlyear has not been assigned a value when I use
IsNothing, isn't that the whole point of using IsNothing? Why does Visual
Studio 2008 give this warning for objects when they are inside of IsNothing?
This is not a high priority or anything I will make a big deal out of, just
wondering if anybody has any comments or suggestions.

As comparison, if you do the same in C# you get a compiler error instead
of a warning. As VB initializes all variables to the default value
(Nothing for reference variables), the compliler doesn't have to protect
you from using uninitialized variables, that's why you only get a
warning in VB.

You should always initialize your variables, or you may get unexpected
results. For example:

Public Sub DisplayValues()
For i As Integer = 1 to 5
Dim x As String
If i = 1 Then x = "One"
If Not IsNothing(x) Then
Console.WriteLine(x)
Else
Console.WriteLine(i)
End If
Next
End Sub

You might expect the output from this to be:

One
2
3
4
5

However, the output is:

One
One
One
One
One

The reason is that the variable x is initialized when it's created, and
that is when you enter the method, not where you have declared the
variable. The variable will not be reinitialized for every iteration in
the loop, so it will retain the value from the first iteration.
 
C

Cor Ligthert[MVP]

Nathan,

Because some people think we only should do it in C# and lobby for that, you
see them even here not knowing how VB is working and then telling that C# is
better (I don't mean Tom).

They forget however that VB has default values.

Cor
 
M

Michel Posseth [MCP]

After rereading your post, i guess i have misunderstood something

Cause what you are describing are the nag IDE warnings of VS , you can do a
few things about this


1. change the IDE check rules
2. set a default value to the object pointer
3. declare the object with the new keyword


small example

will give you the warning as described

Dim foo As foo.lib
If IsNothing(foo) Then

End If


this wil "solve" it

Dim foo As foo.lib=Nothing
If IsNothing(foo) Then

End If



and this wil also solve it

Dim foo As New foo.lib
For i as integer = 0 to 100

if i=50 then

foo.dispose

foo=Nothing

end if

If IsNothing(foo) Then

End If

next



what you see here is a flaw in the IDE warning mechanism , Herfried has
written in the past a few postings regarding this and as it annoyed him so
much he dissabled these warnings in the IDE , personally i would go for
assigning an explicit nothing pointer in the sitautions where i do not
instanciate my object variabels at once



HTH



Michel Posseth
 
M

Michel Posseth [MCP]

Herfried

After i posted the answer i thought after rereading that, i totally
misunderstood what his problem was and that it could be read as if i did not
know that
'String' is a reference type, however i meant that although isnothing can
be used i use string.isnullorempty () on string types .

you are the first who noticed this ;-)

regards

Michel
 
M

Michel Posseth [MCP]

Nathan

see my posting on 10:59 outside this branch of the message tree
 
H

Herfried K. Wagner [MVP]

Nathan Sokalski said:
I have several places in my code in which I use IsNothing(myobject) to test
whether a variable is Nothing. Even though my code compiles and works with
no problems, Visual Studio 2008 is giving me the following warning:

Variable 'ddlYear' is used before it has been assigned a value. A null
reference exception could result at runtime.

Even though it is true that ddlyear has not been assigned a value when I
use IsNothing, isn't that the whole point of using IsNothing? Why does
Visual Studio 2008 give this warning for objects when they are inside of
IsNothing? This is not a high priority or anything I will make a big deal
out of, just wondering if anybody has any comments or suggestions.

I assume you are referring to BC42104. I have disabled the warning because
it's counterproductive in its current implementation. Some time ago I have
written a German article about this warning:

Zur Sinnhaftigkeit der Compilerwarnung BC42104
<URL:http://dotnet.mvps.org/dotnet/articles/bc42104/>

I suggest to disable this warning in the project options.
 
M

Michel Posseth [MCP]

Do i have a elephants memory or not ;-) see my posting on 10:59 yesterday
 
C

Cor Ligthert[MVP]

Michel,

In my idea has Herfried added something, all was it only his opinion which
we shares in this.

He also told that he had made an article, I assume that you as Dutch can
simple translate the name "Sinnhaftigkeit", it is not "Sinful" but simple
something as "Need". We know that too in my idea as Zinvolheid, although
nobody uses that anymore in our language it is simple "Zin".

However, as it is about Herfried repeating something, that comes because he
lives in Austria an is often high in the mountains.

Therefore Armin and I had in past often to warn him to set his Echo Off.

:)

Cor


Michel Posseth said:
Do i have a elephants memory or not ;-) see my posting on 10:59 yesterday
 
J

J.B. Moreno

Nathan Sokalski said:
Variable 'ddlYear' is used before it has been assigned a value. A null
reference exception could result at runtime.

Even though it is true that ddlyear has not been assigned a value when I use
IsNothing, isn't that the whole point of using IsNothing? Why does Visual
Studio 2008 give this warning for objects when they are inside of IsNothing?
This is not a high priority or anything I will make a big deal out of, just
wondering if anybody has any comments or suggestions.

In the particular case of IsNothing the warning is not very use, but
excluding it would make the code that gives the warning more complex
for very little gain -- and possibly some loss.

The reason why it could be a loss is that while an unassigned variable
will have a null value, that's not the only time it can have a null
value. And having an unassigned variable /can/ be an indication of a
bug in the program, even though you're checking for Nothing.
 
H

Herfried K. Wagner [MVP]

J.B. Moreno said:
The reason why it could be a loss is that while an unassigned variable
will have a null value, that's not the only time it can have a null
value. And having an unassigned variable /can/ be an indication of a
bug in the program, even though you're checking for Nothing.

There are no unassigned variables in VB. Variables in VB are either
assigned explicitly or implicitly.
 
J

Jack Jackson

There are no unassigned variables in VB. Variables in VB are either
assigned explicitly or implicitly.

By "unassigned variables", J.B. means not explicitly assigned by code.

While I occasionally have to explicitly initialize a variable to avoid
the warning, all in all I like having the warning to catch those cases
in which I thought all paths through the code set a variable, but they
don't.
 
C

Cor Ligthert[MVP]

This replied Michel yesterday to Herfried

Do i have a elephants memory or not ;-) see my posting on 10:59 yesterday

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top