472,373 Members | 2,009 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

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 2284
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jim Heavey | last post by:
I am trying to transition from VB.Net - what is the equivalent if the IsNothing()? Thanks in advance for your assistance!!!!!!!!!
12
by: Steve Peterson | last post by:
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...
8
by: MattB | last post by:
Hello. I have a vb.net (asp.net) application that uses ado.net datasets. At one point, I need to check a text field in a DataTable to see if there's any text in it before performing text operations...
4
by: Dave Taylor | last post by:
This is really not very important but something that I'm just curious about...which is faster to execute or are they the same: (1) If IsNothing(_myVar) Then ... or (2) If _myVar Is Nothing...
2
by: Renilkumar | last post by:
Hello, I have successfully migrated asp.net application built in VS2003/1.1 to VS2005/2.0 in my local machine. It runs good without any errors. However If I copy the application into the shared...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.