468,126 Members | 1,500 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,126 developers. It's quick & easy.

IsNothing vs. Is Nothing

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
Nov 20 '05 #1
12 43454
* "Steve Peterson" <fa******@fakemail.com> scripsit:
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>
Nov 20 '05 #2
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" <fa******@fakemail.com> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #3
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

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:bp*************@ID-208219.news.uni-berlin.de...
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>

Nov 20 '05 #4
* "Steve Peterson" <fa******@fakemail.com> scripsit:
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(objec t)
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>
Nov 20 '05 #5
"Steve Peterson" <fa******@fakemail.com> schrieb
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.
--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #6
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

Herfried K. Wagner [MVP] wrote:
* "Steve Peterson" <fa******@fakemail.com> scripsit:
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(objec t)
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>

Nov 20 '05 #7
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" <fr*****@nospam.com> wrote in message
news:vr************@corp.supernews.com...
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

Herfried K. Wagner [MVP] wrote:
* "Steve Peterson" <fa******@fakemail.com> scripsit:
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(objec t) 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>


Nov 20 '05 #8
In article <vr************@corp.supernews.com>, frostalicious wrote:
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

Herfried K. Wagner [MVP] wrote:
* "Steve Peterson" <fa******@fakemail.com> scripsit:
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(objec t)
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.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #9
"frostalicious" <fr*****@nospam.com> schrieb
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.

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #10
* "frostalicious" <fr*****@nospam.com> scripsit:
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>
Nov 20 '05 #11
Cor
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
Nov 20 '05 #12
Thanks all to your contributions and insights!
Steve
"Steve Peterson" <fa******@fakemail.com> wrote in message
news:u%****************@TK2MSFTNGP12.phx.gbl...
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

Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Jim Heavey | last post: by
4 posts views Thread by tinman | last post: by
8 posts views Thread by MattB | last post: by
4 posts views Thread by Dave Taylor | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.