views:

569

answers:

5

This is a question about the VB.NET language. Since I am using it every day, I just try to understand the motivations behind some of its constructs.

I just find out that this line :

If myObject Is Nothing then

is as correct as this one is :

If Nothing Is myObject Then

Same results. Using ildasm, we can see that these lines are translated to :

if myObject = null then

and

if null = myObject then

Well, but, in VB.NET, you cannot write :

if myObject = Nothing Then

The compiler will not accept that.

Mmm, to me, If Nothing Is myObject is much more less obvious than If myObject = Nothing.

Why did VB.NET authors just think the opposite ? Any hint ?

+16  A: 

The problem you're running into is that VB.Net differentiates the 2 types of object comparison. Namely Reference and Value comparison.

The "Is" operator in VB.Net is used for reference comparison. This can be used when the values in question are both reference types or nullables. Attempting to compare value types i this manner will result in a compilation error.

The "=" operator is used for Value comparison. The value comparison can only be used on types which define an explicit =, <> operator pair in their class definition. The actual implementation of the equality depends on the implementation of the operator.

C# takes a different approach in that it uses == for both value and reference comparison. Which is used is dependent upon a couple of factors including the type of the values being compared and the implementation of certain equality methods.

JaredPar
I was waiting here counting down for you to come and write an answer. :) I was about to submit mine if you were 10 seconds late.
Mehrdad Afshari
@Mehrdad, glad I hit refresh in time :)
JaredPar
+4  A: 

It is one of these things inherited from VB6 and COM. VB6 makes a distinction between reference type objects (that are instantiable) and native types such as int. Reference types had to be created and assigned with the "Set" operator whereas native types simply used "=".

Otávio Décio
+1  A: 

Well, in some cases you can write If myObject = Nothing Then, but it will not be a null comparison:

Dim someValue As Integer
If someValue = Nothing Then
    ' do something '
End If

The above code is equivalent of the following:

Dim someValue As Integer
If someValue = 0 Then
    ' do something '
End If

In this case you have a value type that has its "emtpy" value (0 for an Integer, Point.Empty for a Point structure as examples). The Is operator performs a reference comparison (checking whether the compared objects are the same instance), while the equal sign is a value comparison.

Fredrik Mörk
Yes, just tried it : you can write : " if 2 = Nothing Then ", the compiler is just ok with that. Moreover, as you said, " if 0 = Nothing " is always true... Great, thanks !
Sylvain
I think this is a confusing part of the VB language. To a newcomer it's not clear that Nothing means "default value" for value types...
Meta-Knight
+1  A: 

The use of Is Nothing is a construct that goes back to the days of classic Visual Basic (i.e. 6.0 and below).

To maintain some semblence of compatibility (just like the way VB6 handles non-lazy evaluation of the If statement) this has been carried over into VB.NET.

Fortunately as ocdecio points out, there were other similar peccadillos which have not been carried forward into VB.NET such as the Set statement for assigning objects to variables.

Kev
+1  A: 

Another thought is VB is intended to be more verbose if you look at some of the loop and conditional syntax. A couple of examples:

VB:

If x==0 Then
End If

While 
End While

For i = 0 to 10 Step 2
Next i

C#

 If x==0 
 {
 }

 While 
 {
 }

 for (int i = 0; i<=10;i+2)
 {
 }

See all the extra words in the VB.Net code? That is another possible reason for the difference.

JB King
Well, I'm a C, C++, C# fan who have to write code using VB.NET now... :( But, all in all, it's not bad at all : VB.NET is really verbose, but it is an other language and I have a great time trying to find its flaws and its strengths. Thank :)
Sylvain
I experienced this shift a couple of times. The first going from C/C++ ISAPI extensions to VBScript in classic ASP and then again in going from VBScript to C#.Net in ASP.Net so I have seen this a couple of times now but I've written enough code in various versions of BASIC and C languages that I can flip back and forth pretty easily.
JB King
If we're counting characters, you've omitted the compulsory parentheses on a C# if statement.
MarkJ