Well thank you for the clarification
As i said the code was not mine , we just noticed some strange behavior in
a routine that was written by a third party from wich we obtained the
source and discovered these strange constructs , as these were just integer
values i would personally just check the values ( i 0 ) or even bether
use a nullable of integer and check the hasvalue property ( cause in the
case of this routine 0 could have been valid ) .
When we were discussing these constructs , and tried if we might have been
missing something :-) we discovered the difference as mentioned , my
collegue asked me for a explanation wich i had not ready at that moment ,
i am happy that the cavalary has arived with such a strong force :-) thank
you very much .
The coder by the way used i = Nothing wich is the same as i=0 ( wich i
would have prefered ) , in the end this also turned out to be the bug as i
could be 0 and he was trying to check if the value was suplied .
regards
Michel Posseth
"Armin Zingler" <az*******@freenet.deschreef in bericht
news:%2****************@TK2MSFTNGP04.phx.gbl...
"AMercer" <AM*****@discussions.microsoft.comschrieb
Dim i As Integer = 0
MsgBox(i = Nothing)
and
MsgBox(IsNothing(i))
produce a different result , could someone explain me why ?
IsNothing() takes an object argument, so IsNothing(i) boxes the
scalar i into an object and then evaluates IsNothing on the newly
created object. IsNothing(x) will return false for any value
semantics item x. Thus, IsNothing(i) above is poor programming
because (1) it always returns false even if i is nonzero, and (2) it
does an time consuming box operation.
Nothing is the default value for any data type, and that includes
both object and value types. Thus i=Nothing is the same as i=0. I
believe the compiler takes care of this at compile time, so there
should be no performance hit. In theory, there is nothing wrong
with this kind of construct, but IMO it is poor programming style.
I think it is best to use Nothing with object types and not with
value types, but others may disagree.
I completeley agree with everything you wrote. :) Only adding that using
"Is
Nothing" (which only makes sense with reference types either) should be
preferred to calling IsNothing.
Armin