Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Not ... Is Nothing" versus "... IsNot Nothing"

Tags:

vb.net

nothing

Does anyone here use VB.NET and have a strong preference for or against using Not foo Is Nothing as opposed to foo IsNot Nothing? If so, why?

For Example

If var1 IsNot Nothing Then ... End If 

and

If Not var1 Is Nothing Then ... End If 

I just want to know which one is better?
Are they both equally acceptable?

like image 894
nnnn Avatar asked Sep 20 '13 06:09

nnnn


People also ask

What is not nothing in VB?

IsNothing is intended to work on reference types. It returns True if the expression represents an object variable that currently has no object assigned to it; otherwise, it returns False.

Is nothing in Visual Basic?

For strings in Visual Basic, the empty string equals Nothing . Therefore, "" = Nothing is true. If you declare a variable without using an As clause and set it to Nothing , the variable has a type of Object . An example of this is Dim something = Nothing .

Is nothing vs nothing vb net?

"IsNothing is intended to work on reference types. A value type cannot hold a value of Nothing and reverts to its default value if you assign Nothing [...] IsNothing always returns False." But Nothing "Represents the default value of any data type.

Is nothing Excel VBA?

Checking if an object is nothingThere is no isNothing() function in VBA (see is… () functions). Also, the function typeName(obj) returns the string "Nothing" if obj is nothing .


2 Answers

The

If Not var1 Is Nothing Then 

Is a hangover from VB6. There didn't used to be an IsNot, and so this was the only way to determine if a variable was not Nothing. It seems to be redundant in VB.NET.

like image 121
Paul Michaels Avatar answered Sep 28 '22 18:09

Paul Michaels


foo IsNot Nothing

The following line is straight from Microsoft's Visual Basic Coding Conventions:

Use the IsNot keyword instead of Not...Is Nothing.

like image 45
Tony L. Avatar answered Sep 28 '22 18:09

Tony L.