Is = overloaded for Is when an operand is nothing?

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I noted with interest that typing:

?Nothing = Nothing

into the immediate window returns true..
So why do i write in code:

If x Is Nothing


are there special rules or quirks that allow the immediate staement to return true, but coding If x = Nothing return false? If = is overloaded, why do we bother with "Is" at all?
 
I would guess that in order for = to work, both sides must be of the same type. Since X is a different type from Nothing.... you can't do a simple =....

I'm just guessing here.... but it makes sense.

-tg
 
Docs: "The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons."

= as comparison operator compares values.

So, if you thought that Nothing was the value of an unreferenced object variable, that is wrong, it is merely a reference to the object state Nothing, which represents an unreferences object variable. It does sound a bit like chasing its own tail, but its not.

Docs again: "If the variable is of a reference type — that is, an object variable — Nothing means the variable is not associated with any object."
 
From what I've read, I was inclined to think that = was overloaded for Object.Equals in the majority (if not all) cases. Object does a quick reference/memory address check and if two references are the same, Object.Equals deems them equal.. Some types, like String, override this behaviour to implement per-character equivalence checking or something simialr, hence = can be used to determine whether the content of two strings is identical, even if they live at separate areas of memory and are distinct instances.

Where i got really confused, was with Nothing = Nothing being allowed, whereas Nothing.Equals(Nothing) will inherently fail, and I havent so far seen an Object definition for Nothing (that is to say, it doesnt appear to be implemented in the language as an object type) and this notion is persisted into other OO languages.

So it begs the question; is Nothing a type? Or in a similar way to Nullable, can it/is it modified under the skin to be typed in a way we never see?

I'm not sure I understand the initial guess that for = to work, types must be identical - at what level in the hierarchy are we talking about? Pretty much everything in VB is the same type as anything else if taken back to the Object root, and Objects can be compared with =. Similarly, its reasonable to say in an event handler:

If sender = myButton

in this case, are you asserting that it is because the "sender as Object" is actually of type button, that the comparison can succeed? MY initial inkling would be that it is the other way round; thanks to the fact that myButton can be demoted to Object, and Object's = used (in the absence of an overriding Button.Equals).. And I get this thinking from some curiously written event handlers that work like:
If sender = myButton then 'do the button thing
Else If sender = myCheckBox Then 'do the checkbox thing

sender is either a button or a checkbox, but this code wont break.. Which leads me to believe that the myButton/myCheckbox is demoted to Object and a reference equivalence performed..

Which i guess, brings me to "what is Nothing d/promoted to in the case of ?Nothing = Nothing" and is it special in this regard? If effort was made to have this special, why not carry it through and just have If myButton = Nothing Then myButton = New Button()
 
JohnH said:
Docs: "The Is operator determines if two object references refer to the same object. However, it does not perform value comparisons."

= as comparison operator compares values.

I had in mind that i's seen that somewhere.. i think when reading a blog about IsNot written by one of the VS developers.. So, i find it quite logical that Is is used for reference comparison, and = used for value comparison.. but does that mean the reference and value of the pointer?
(In the same way that ByRef and ByVal refer to how a pointer is passed)

If = is used for comparing values, does it do this via use of .Equals() ? If not, then how does it deal with the potentially millions of operand types it might encounter due to polymorphism? One would expect that it is written into the language *using* polymorphism, so as to escape being a victim of it ("chasing its own tail"?)

Does nothing thus have a value? How is the expression ?Nothing = Nothing evaluated at all? If an object reference points to nothing, how does the CLR implement this? Does Nothing live on the heap or the stack (logic suggests it lives nowhere, but is only used win conjuction with heap data)? What would we find on the heap, and can this be used to explain the ability to evaluate Nothing=Nothing, yet the inablilty to evaluate x=Nothing? And finally, why is Nothing=Nothing actually true? It's behaving like a static reference to something?
 
Is operator does not have anything to do with values, only references.

The docs knows Object.Equals method:
"The default implementation of Equals supports reference equality only, but derived classes can override this method to support value equality.
For reference types, equality is defined as object equality; that is, whether the references refer to the same object. For value types, equality is defined as bitwise equality. The ValueType class supports value types."

ValueType.Equals(Object) is the complement here and returns "true if obj and this instance are the same type and represent the same value"
 
So, youre saying that = always compares by value, and Is always compares by reference, and .Equals() does whatever it is programmed to do (which is by deafult, reference compare in Object and its descendants)?

So my question is still "how can = compare by value when the comparison is complex? How does = defeat the millions of permutations that polymorphism introduces?"

And i still havent seen an answer to how ?Nothing = Nothing is true, or why it works in the first place? Is it a special case built into the language?
 
cjard said:
So my question is still "how can = compare by value when the comparison is complex? How does = defeat the millions of permutations that polymorphism introduces?"
No sure what you mean by this question; the default = comparison operator is not supported for reference types, only exception is the special string comparisons supported as explained in docs. Other than that you can override the = as comparison operator in derived classes, but then you implement the specific comparison yourself.
 
i've come to the following understanding:


Is compares references, for reference types. It cannot be used on value types
= compares values for value types. It cannot be used on reference types unless it is overloaded specifically to do so. String is an example of a default overloading provided by MS
.Equals() usually compares references thanks to the default implementation in Object using Is. The method can be overridden to provide customised comparison.


Which means that = has been overloaded specifically to compare Nothing=Nothing. Does it mean Nothing is a (static) value treated specially by the CLR?
 
No, I don't think so, but rather that if you do Nothing=Nothing then since = is looking for value types on both sides of equation it will interpret Nothing in this case as the default Integer value which is zero, thus the equation is interpreted as 0=0. Nothing always represent the default value of any type variable instance. Don't confuse this with what I said in previous post about reference type instances, there Nothing/"default value" is a non-referenced object and not the value of the object.
 
Back
Top