VB.NET language and Nothing : why is it the way it is?
-
06-07-2019 - |
Question
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 ?
Solution
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.
OTHER TIPS
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 "=".
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.
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.
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.