Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / October 2007

Tip: Looking for answers? Try searching our database.

curious  about something

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Michel Posseth [MCP] - 03 Oct 2007 09:51 GMT
Hello

i just encountered a funny situation

Dim i As Integer = 0

       MsgBox(i = Nothing)

and

       MsgBox(IsNothing(i))

produce a different result , could someone explain me why ?
 
p.s.

The code i encountered this problem with is not mine :-)  , but we just
discovered a bug in code written by a third party developer , after a brief
discussion i could not
explain why it produces a different result so i call on the cavalery :-)
AMercer - 03 Oct 2007 11:32 GMT
>  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.
Armin Zingler - 03 Oct 2007 12:07 GMT
> >  Dim i As Integer = 0
> >         MsgBox(i = Nothing)
[quoted text clipped - 16 lines]
> 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
amdrit - 03 Oct 2007 14:57 GMT
Hold on you can't ask if nothing equals a reference type or object.

i = Nothing  <-- is invalid.  Nothing ever equals anything even if the
anything IsNothing.  I guess you can say Nothing is not equatable.

dim x as object
msgbox (x is Nothing)
msgbox (IsNothing(x))

While in c like languages you may:
if (null == x){} <-- you cannot do this in vb

>> >  Dim i As Integer = 0
>> >         MsgBox(i = Nothing)
[quoted text clipped - 23 lines]
>
> Armin
Armin Zingler - 03 Oct 2007 15:35 GMT
> Hold on you can't ask if nothing equals a reference type or object.
>
[quoted text clipped - 8 lines]
> While in c like languages you may:
> if (null == x){} <-- you cannot do this in vb

Sorry, I don't get the point. My point is: I would never write
"IsNothing(x)" because it's more overhead than "Is Nothing". Why call a
function that does a comparison instead of straigthly doing the comparison?

Armin
amdrit - 03 Oct 2007 18:39 GMT
My comment was directed at the OP and not towards anything you or AMercer
wrote.  I apologize for the confusion.

His question was what is the difference in

given: dim i as integer

msgbox(i=nothing)
msgbox(isnothing(i))

my point is i=nothing will always be true when i=0.  The test is invalid
conceptually, since nothing is the absence of a value and 0 is a value.
Asking if something is Nothing denotes "has this variable been initialized
or not?"  Since all primitive types are initialized by VB upon declaration
they are never Nothing.  Even a String.Empty can be misconstrued as Nothing
even though the String.Empty = "" which is not Nothing.

1-1 = 0 'It doesn't equal Nothing.

I think what happens with the i=Nothing test is VB is implicitly converting
Nothing to the same "Object type" as i.  Since VB always initializes
integers to 0, i=Nothing returns true and we can verify that with the
IsNothing(i) test.  This happens even with Option Explicit and Option Strict
both turned on.

>> Hold on you can't ask if nothing equals a reference type or object.
>>
[quoted text clipped - 15 lines]
>
> Armin
Michel Posseth  [MCP] - 03 Oct 2007 18:05 GMT
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

>> >  Dim i As Integer = 0
>> >         MsgBox(i = Nothing)
[quoted text clipped - 23 lines]
>
> Armin

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.