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?
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.
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 .
"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.
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 .
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.
foo IsNot Nothing
The following line is straight from Microsoft's Visual Basic Coding Conventions:
Use the
IsNot
keyword instead ofNot
...Is Nothing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With