49

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?

Gary Barrett
  • 1,764
  • 5
  • 21
  • 33
nnnn
  • 1,041
  • 3
  • 18
  • 35
  • 5
    I've already looked this Question.[It](http://stackoverflow.com/q/5791/507860) is a Comparism of IsNothing(obj) Between obj Is Nothing. – nnnn Sep 20 '13 at 06:21

5 Answers5

56

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.

Paul Michaels
  • 16,185
  • 43
  • 146
  • 269
  • 4
    `IsNot` is a newer operator that didn't exist in pre .NET2 versions. – Tim Schmelter Apr 23 '14 at 09:30
  • 2
    I wonder if it's actually faster or compiles to the same. I mean, `Not x Is Nothing` first does a check for type equality, then negates it. `x IsNot Nothing` does only a check for type unequality which the runtime might be capable of doing faster (dumbly imaginable as if it checks a type field by field and can leave by the first non-matching one rather than going through each field to check for equality - on top just to negate the result eventually). – Ray Oct 12 '16 at 12:31
17

foo IsNot Nothing

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

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

Tony L.
  • 17,638
  • 8
  • 69
  • 66
7

I would go with the first variant - it reads like English and is easier to follow/understand than the second one. Other than that, they are equivalent.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
3

I found a similar question here VB.NET - IsNothing versus Is Nothing, where I feel this question was exhaustively answered. Among the answers Jack Snipes identified http://weblogs.asp.net/psteele/410336, a blog that gives some extra detail. From those I prefer and have used

IsNot Nothing

which also makes my code easier to read and understand.

Community
  • 1
  • 1
Muzoora Savior
  • 529
  • 5
  • 16
  • 3
    "Not...Is Nothing" is not the same thing as "Not IsNothing(...)" Not outright wrong, but not an answer for this question. – EKW Feb 03 '16 at 18:55
-1

Using VB 7.0

If var1 Is Not Nothing Then

generates an "invalid use of object error" as per this "VBForums" link.

If var1 IsNot Nothing Then

generates a "Compile error: Expected: Then or GoTo"

If Not IsNothing(var1) Then

worked like a champ

MikeF
  • 764
  • 9
  • 26
  • 2
    "`If var1 IsNot Nothing Then` generates a Compile error" of course it does, when there is no statement or block afterwards. `var1 IsNot Nothing` itself will work. You also fail to show what `Not var1 Is Nothing` does, so you don't really answer the question. And please fix your formatting – Breeze Sep 22 '16 at 06:35