IsNothing vs. Is Nothing

  • Thread starter Thread starter Steve Peterson
  • Start date Start date
S

Steve Peterson

Hi - just a quick question.

I was wondering which is the better "VB.Net" way - to use IsNothing or the
Is Nothing in an If..Then statement

For example:

If IsNothing(myObject) then
'blah blah blah..
End If

vs

If myObject Is Nothing then
'blah blah blah..
End If

Thanks
Steve
 
* "Steve Peterson said:
I was wondering which is the better "VB.Net" way - to use IsNothing or the
Is Nothing in an If..Then statement

For example:

If IsNothing(myObject) then
'blah blah blah..
End If

vs

If myObject Is Nothing then
'blah blah blah..
End If

Did you have a look at the MSIL code of the compiled executable using
"ildasm.exe"?

;-)

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Steve,
I don't believe either is really "better" then the other.

I prefer myObject Is Nothing, however that does not make it "Better".

I would be more concerned with being consistent within the project,
solution, team, company.

Hope this helps
Jay
 
Steve Peterson said:
Hi - just a quick question.

I was wondering which is the better "VB.Net" way - to use IsNothing
or the Is Nothing in an If..Then statement

For example:

If IsNothing(myObject) then
'blah blah blah..
End If

vs

If myObject Is Nothing then
'blah blah blah..
End If

I didn't know IsNothing. I use the second version because I don't need a
function for this comparison.
 
Hi Herfield

Mmm, I guess if I had, or even had a clue what you are talking about, I
probably wouldn't have posted my question in the first place..

If you would be so kind as to decipher your answer a bit, maybe I (and
others) would have some valuable insight for the future on our road to being
better .Net programmers...

Thanks
Steve
 
* "Steve Peterson said:
Mmm, I guess if I had, or even had a clue what you are talking about, I
probably wouldn't have posted my question in the first place..

If you would be so kind as to decipher your answer a bit, maybe I (and
others) would have some valuable insight for the future on our road to being
better .Net programmers...

This code

\\\
Dim x As Form
If x Is Nothing Then
Beep()
End If
If IsNothing(x) Then
Beep()
End If
///


compiles to this MSIL:

\\\
IL_0000: nop
IL_0001: ldloc.0
IL_0002: brtrue.s IL_000a
IL_0004: call void [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0009: nop
IL_000a: nop
IL_000b: ldloc.0
IL_000c: call bool [Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_0011: brfalse.s IL_0019
IL_0013: call void [Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0018: nop
IL_0019: nop
IL_001a: nop
IL_001b: ret
///

As you can see, the 2nd method requires a call to the 'IsNothing'
object. This may reduce performance.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Thanks for the fish, but I'd like to be a fisherman. =`:^>

How do you run the disassembler to get those answers?
From the command prompt? As a Windows program?

I can launch ildasm.exe as a Windows app, but can't
figure out how to see the IL lines. Would be nice!

-- frosty
* "Steve Peterson said:
Mmm, I guess if I had, or even had a clue what you are talking
about, I probably wouldn't have posted my question in the first
place..

If you would be so kind as to decipher your answer a bit, maybe I
(and others) would have some valuable insight for the future on our
road to being better .Net programmers...

This code

\\\
Dim x As Form
If x Is Nothing Then
Beep()
End If
If IsNothing(x) Then
Beep()
End If
///


compiles to this MSIL:

\\\
IL_0000: nop
IL_0001: ldloc.0
IL_0002: brtrue.s IL_000a
IL_0004: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0009: nop IL_000a: nop
IL_000b: ldloc.0
IL_000c: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_0011: brfalse.s IL_0019
IL_0013: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0018: nop IL_0019: nop
IL_001a: nop
IL_001b: ret
///

As you can see, the 2nd method requires a call to the 'IsNothing'
object. This may reduce performance.

<http://www.plig.net/nnq/nquote.html>
 
frostalicious said:
Thanks for the fish, but I'd like to be a fisherman. =`:^>

How do you run the disassembler to get those answers?
From the command prompt? As a Windows program?

I can launch ildasm.exe as a Windows app, but can't
figure out how to see the IL lines. Would be nice!


You can enter ildasm in the help index and will find it's documentation.

But to answer your question: After starting ildasm.exe, open select your Exe
file and you can browse the content and view the IL code.
 
frostalicious,
Use ildasm.exe.

For VS.NET 2003 it can be found at:
C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\ildasm.exe

For VS.NET 2002 it can be found at:
C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Bin\ildasm.exe

Normally I set up a new "External Tool" in VS.NET to invoke it.

For example in VS.NET 2003 I have:
Title: IL Dasm
Command: C:\Program Files\Microsoft Visual Studio .NET
2003\SDK\v1.1\Bin\ildasm.exe
Arguments: /adv $(TargetPath)
Initial directory: $(TargetDir)

Which will open the assembly for the current project.

Hope this helps
Jay

frostalicious said:
Thanks for the fish, but I'd like to be a fisherman. =`:^>

How do you run the disassembler to get those answers?
From the command prompt? As a Windows program?

I can launch ildasm.exe as a Windows app, but can't
figure out how to see the IL lines. Would be nice!

-- frosty
* "Steve Peterson said:
Mmm, I guess if I had, or even had a clue what you are talking
about, I probably wouldn't have posted my question in the first
place..

If you would be so kind as to decipher your answer a bit, maybe I
(and others) would have some valuable insight for the future on our
road to being better .Net programmers...

This code

\\\
Dim x As Form
If x Is Nothing Then
Beep()
End If
If IsNothing(x) Then
Beep()
End If
///


compiles to this MSIL:

\\\
IL_0000: nop
IL_0001: ldloc.0
IL_0002: brtrue.s IL_000a
IL_0004: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0009: nop IL_000a: nop
IL_000b: ldloc.0
IL_000c: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_0011: brfalse.s IL_0019
IL_0013: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0018: nop IL_0019: nop
IL_001a: nop
IL_001b: ret
///

As you can see, the 2nd method requires a call to the 'IsNothing'
object. This may reduce performance.

<http://www.plig.net/nnq/nquote.html>
 
Thanks for the fish, but I'd like to be a fisherman. =`:^>

How do you run the disassembler to get those answers?
From the command prompt? As a Windows program?

I can launch ildasm.exe as a Windows app, but can't
figure out how to see the IL lines. Would be nice!

-- frosty
* "Steve Peterson said:
Mmm, I guess if I had, or even had a clue what you are talking
about, I probably wouldn't have posted my question in the first
place..

If you would be so kind as to decipher your answer a bit, maybe I
(and others) would have some valuable insight for the future on our
road to being better .Net programmers...

This code

\\\
Dim x As Form
If x Is Nothing Then
Beep()
End If
If IsNothing(x) Then
Beep()
End If
///


compiles to this MSIL:

\\\
IL_0000: nop
IL_0001: ldloc.0
IL_0002: brtrue.s IL_000a
IL_0004: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0009: nop IL_000a: nop
IL_000b: ldloc.0
IL_000c: call bool
[Microsoft.VisualBasic]Microsoft.VisualBasic.Information::IsNothing(object)
IL_0011: brfalse.s IL_0019
IL_0013: call void
[Microsoft.VisualBasic]Microsoft.VisualBasic.Interaction::Beep()
IL_0018: nop IL_0019: nop
IL_001a: nop
IL_001b: ret
///

As you can see, the 2nd method requires a call to the 'IsNothing'
object. This may reduce performance.

<http://www.plig.net/nnq/nquote.html>

Launch ildasm.exe. Go to file->open, navigate to the assembly your
intrested in, double click it. You sill see a list of all the defined
types (assuming you chose a .NET app :), then find the class.member your
interested in and double click it.
 
* "frostalicious said:
Thanks for the fish, but I'd like to be a fisherman. =`:^>

How do you run the disassembler to get those answers?
From the command prompt? As a Windows program?

From the Visual Studio .NET command prompt. Just start the "ildasm.exe"
tool and open the binary.
I can launch ildasm.exe as a Windows app, but can't
figure out how to see the IL lines. Would be nice!

Just doubleclick a procedure selected from the treeview in the main window.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
Hi Steve,

Reading at Armin answers, I had the same as he and we both have seen a lot
of discussions about "Nothing" in this newsgroup (I have to write "Nothing"
like this from Armin).

But we have also seen a lot of questions/discussions about keywords which
are appearing and disappearing.

If there is no need for a keyword I never would use it. Mostly it gives you
not a very big advantage and the possibility always exist that in a future
release (is it not the next but maybe some after that) that keyword
disappears and you have to change your program anyway.

Just a thought

Cor
 
Back
Top