469,640 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

IsNothing alternative ?

Hi....

There appears to be another way of checking if an object IsNothing in
..NET.....was wondering if this approach is better than the classic VB6 way
of checking Is Nothing ?

Example
******

Public Function IsNothing(ByVal ExternalObject As Object) As Boolean
Try
Return (ExternalObject.Equals(New System.Object))
Catch ex As NullReferenceException
Return True
End Try
End Function

Private Sub Testing()
Dim objX as ClassX
Debug.WriteLine IsNothing(objX) 'Returns True
objX = New ClassX
Debug.WriteLine IsNothing(objX) 'Returns False
objX = Nothing
Debug.WriteLine IsNothing(objX) 'Returns True
End Sub
Nov 20 '05 #1
4 2111
"tinman" <dw****@yahoo.com> schrieb
Hi....

There appears to be another way of checking if an object IsNothing
in .NET.....was wondering if this approach is better than the
classic VB6 way of checking Is Nothing ?


The "classic" VB6 way is the same as in VB.Net:

if var Is Nothing then

--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
Hi tinman,

Use "Is Nothing". The "Is Nothing" construct checks the value of the
reference directly, which is very fast. In your example, you are either
doing an unnecessary function call, or causing an exception, which is much
worse performance-wise.

Regards,
Daniel

"tinman" <dw****@yahoo.com> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
Hi....

There appears to be another way of checking if an object IsNothing in
.NET.....was wondering if this approach is better than the classic VB6 way
of checking Is Nothing ?

Example
******

Public Function IsNothing(ByVal ExternalObject As Object) As Boolean
Try
Return (ExternalObject.Equals(New System.Object))
Catch ex As NullReferenceException
Return True
End Try
End Function

Private Sub Testing()
Dim objX as ClassX
Debug.WriteLine IsNothing(objX) 'Returns True
objX = New ClassX
Debug.WriteLine IsNothing(objX) 'Returns False
objX = Nothing
Debug.WriteLine IsNothing(objX) 'Returns True
End Sub

Nov 20 '05 #3

"tinman" <dw****@yahoo.com> wrote in message
news:uA**************@TK2MSFTNGP10.phx.gbl...
Hi....

There appears to be another way of checking if an object IsNothing in
.NET.....was wondering if this approach is better than the classic VB6 way
of checking Is Nothing ?

Example
******

Public Function IsNothing(ByVal ExternalObject As Object) As Boolean
Try
Return (ExternalObject.Equals(New System.Object))
Catch ex As NullReferenceException
Return True
End Try
End Function


No that's no good. Exceptions are relatively expensive to catch, and so you
don't want to use exceptions to trap common conditions like this.

Simple object reference equality will do the trick here.

if ExternalObject is nothing ...
David
Nov 20 '05 #4
* "tinman" <dw****@yahoo.com> scripsit:
There appears to be another way of checking if an object IsNothing in
.NET.....was wondering if this approach is better than the classic VB6 way
of checking Is Nothing ?


\\\
If ... Is Nothing Then
...
End If
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

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