= nothing

froodley

Active member
Joined
Apr 20, 2010
Messages
26
Programming Experience
1-3
Hi, all,

I'm just wondering what is actually happening when you do a comparison like "if myobject = nothing then..." in vb.net.

I know it doesn't work, and use IsNothing, etc... i'm just curious what it's doing. why does it evaluate to true? if it's using the object.Equals method, that compares the memory address of the two objects, yes?
 
You seem to still be missing the point. The Is and IsNot operators test referential equality, whereas the = and <> operators test value equality. They are NOT the same thing. All reference types support referential equality tests. This:
VB.NET:
If myVar Is Nothing Then
Test whether myVar refers to an object or not. Value equality is different. In order to perform a value equality test against Nothing, the type of the object being tested must support a conversion of Nothing to that type. The most common such type is the String class. To demonstrate the difference between the two, try running this code:
VB.NET:
Dim str1 As String = Nothing
Dim str2 As String = String.Empty
Dim str3 As String = "Hello World"

If str1 Is Nothing Then
    MessageBox.Show("str1 does NOT refer to an object")
Else
    MessageBox.Show("str1 DOES refer to an object")
End If

If str1 = Nothing Then
    MessageBox.Show("str1 IS equal to Nothing")
Else
    MessageBox.Show("str1 is NOT equal to Nothing")
End If

If str2 Is Nothing Then
    MessageBox.Show("str2 does NOT refer to an object")
Else
    MessageBox.Show("str2 DOES refer to an object")
End If

If str2 = Nothing Then
    MessageBox.Show("str2 IS equal to Nothing")
Else
    MessageBox.Show("str2 is NOT equal to Nothing")
End If

If str3 Is Nothing Then
    MessageBox.Show("str3 does NOT refer to an object")
Else
    MessageBox.Show("str3 DOES refer to an object")
End If

If str3 = Nothing Then
    MessageBox.Show("str3 IS equal to Nothing")
Else
    MessageBox.Show("str3 is NOT equal to Nothing")
End If
If you run that code you'll see that str1 and str3 behave exactly as you'd expect, but the actual behaviour of str2 may not be what you expect. The results say that str2 DOES refer to an object but that is IS also equal to Nothing. That's because, when testing for value equality, Nothing is actually converted to a String first. You can only compare values of two objects of the same type. Converting Nothing to a String returns an empty String. str2 is also an empty string so the two values are equal.

To summarise, use = or <> to compare values, which means for all value types and for reference types that support value equality, and use Is or IsNot when testing references. This is true whether comparing to Nothing or not. Note that, when testing value equality against Nothing for value types, just like with the String class, Nothing must be converted first into a value with the same type as the other value you're comparing. For numeric types that will be zero, and all other value types have their own default value too.
 
Hmmm... I didn't actually see that there was a second page before posting. Sorry if I repeated what anyone else had already said.

It's also worth noting what Nothing actually is. Nothing is a location on the stack that contains all zeroes. If that stack location is a reference then it represents a memory address and an address of zero represents no object. If the stack location is a value then what zero means depends on the type. For numeric types it is the number zero, for Boolean it is False, for Date it is 1/01/0001, etc. For complex value types it is an instance where each field has its default value.
 
Back
Top