= 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?
 
IsNothing returns a Boolean value indicating whether an expression has an object associated with it.

Object.Equals returns reference equality for reference types meaning that it's checking if they're both pointing to the same object and bitwise equality for value types meaning they have the same binary representation.

For further information I'd suggest going to the msdn pages for each:

IsNothing Function

Object.Equals Method (Object) (System)
 
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?
Well actually to check if something is nothing or not you wouldn't use an equals sign at all, you'd use the Is or the IsNot keywords, like:

If YourObject Is Nothing Then ...
If YourObject IsNot Nothing Then...

What the "Is" and the "IsNot" keywords does is check if the object has a reference (memory pointer or not) if it does have one then evaluation returns either True or False depending on which keyword you use.
 
Thanks, I do know to use IsNothing. I am just wonder what = Nothing does by comparison. :confused:
You don't use YourObject = Nothing unless you want to set it to to Nothing. The reason is because it'll never equal nothing, it either is nothing or it is not nothing.

Edit:
Your code actually wont compile if you try to see if YourObject = Nothing, I just tried:
VB.NET:
Dim tb As TextBox = Nothing
MessageBox.Show(CStr(tb = Nothing))
and I get a compile error (even with Option Strict Off)
 
Try this:

Dim x As New Integer
MsgBox(x)
x = 0
MsgBox(x)
If x = Nothing Then
MsgBox("fail")
Else
MsgBox(x)
End If


You'll get 0, 0, fail. If you assign a value other than 0, you get ,eg, 7,7,7. Without the assignment (just x as new integer, then if x= nothing) you get fail.
 
Last edited:
Right, because Integer is a value type and not reference type, so it'll never be "Nothing", the closest to Nothing you can get is 0 (zero) which is still a value.

Why would you even want to see if a value type is nothing in the first place? That doesn't even make sense.
 
That's because when you set an Integer equal to Nothing, it's setting it equal to 0 (zero) because you can't set a value type to a null reference, it's not possible. Then you check that integer if it's equal to Nothing, what's really going on is it's checking it if it's equal to 0 (zero) and since 0 = 0 it runs the code in the true part which shows your messagebox that says "fail".

Try running that code using an object, not a value and see what happens.
 
Yeah, I see that now, sorry... I thought I had canceled that last post? Darn internet...

But you can do x = Nothing comparisons for reference types as well, and it works as you'd expect as long as the type implements the = operator and just return object.equals(a,b)... eg, object.equals(x, nothing) returns the expected value...

so why do we need isnothing?
 
so why do we need isnothing?
the IsNothing() function is in the Microsoft.VisualBasic namespace, which is primarily filled with legacy vb6 stuff. I haven't used anything in it in .Net (don't see a need to). Ultimately I just use the Is keyword to see if I've instantiated an object or not and it works with all reference types.
 
easy way to think about it:

use = nothing for a value. values use =
use IS nothing when its an object, class, anything that is not a simple value. an objects IS something, and that something can be nothing

its a simple way to remember (although technically its not correct, the other posts above explain it in much better detail)
 
Right, because Integer is a value type and not reference type, so it'll never be "Nothing", the closest to Nothing you can get is 0 (zero) which is still a value.

Why would you even want to see if a value type is nothing in the first place? That doesn't even make sense.

To be more precise; value types cannot hold a value of nothing and revert back to their default value if you try and assign Nothing to it.
 
that's just different paradigm approaches to the problem:

Operational:

= (== in C#) - an operational check, though really only works with value types because the = operator usually isn't defined for reference types
Is, IsNot (==, != in C#) - an explicit reference operation defined for reference types. It's a hold over from earlier versions of VB which verbosely creates a distinction between value types and reference types. Note how C# just uses the same operators for both.

Functional:
IsNothing(...) - functional entry that checks Nothing explicitly

Object:
Object.Equals(...) - object defined equality, accessed as a member of the object.


They all also compile slightly different. The operators tend to attach to the object's defined Equals method. Where as the functional method attaches to... err, I don't know. Never dug into the IL to see. As far as I know the .Equals is the fastest way.



On a side note I hate how vb uses "=" for both assigning AND for equality testing.
 
= (== in C#) - an operational check, though really only works with value types because the = operator usually isn't defined for reference types
Is, IsNot (==, != in C#) - an explicit reference operation defined for reference types. It's a hold over from earlier versions of VB which verbosely creates a distinction between value types and reference types. Note how C# just uses the same operators for both.
In C/C++, Java, Javascript, C# you have:
= assigns the value (same as = in vba/vb6/vb.net)
== returns a boolean if the values are the same or all of the values are of 2 classes are the same (same as = in vba/vb6/vb.net)
=== returns a boolean if the two object have the same pointer (same as the Is keyword in vba/vb6/vb.net)
On a side note I hate how vb uses "=" for both assigning AND for equality testing.
Not on the pointer level, but yes
 
Back
Top