473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

curious about something

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 :-)

Oct 3 '07 #1
6 1245
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.

Oct 3 '07 #2
"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

Oct 3 '07 #3
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


"Armin Zingler" <az*******@freenet.dewrote in message
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

Oct 3 '07 #4
"amdrit" <am****@hotmail.comschrieb
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

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

Oct 3 '07 #5
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

Oct 3 '07 #6
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.

"Armin Zingler" <az*******@freenet.dewrote in message
news:Oe**************@TK2MSFTNGP06.phx.gbl...
"amdrit" <am****@hotmail.comschrieb
>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


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

Oct 3 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Randell D. | last post by:
Folks, A ng poster recently questioned their usage/creation of arrays and their correct syntax. I got the idea to performance test from a recent (excellent) PHP Tutorial article that was in Linux...
1
by: Player | last post by:
Hell all once again :) I just installed Python after giving Activestates distribution of python a try for a few days, and I have come across something that strikes me as a little weird, to my...
40
by: Confused User | last post by:
I am curious what the origins of size_t are. I see that it is usually typedef'd to be the native integer size of a particular machine. I also see that routines like strncpy(char *t, const char *s,...
7
by: tshad | last post by:
I was curious if there is some reason why you don't need the "then" in the if test of VB.Net or is it just ASP.NET. I just noticed that I don't really need to explicitly put the word "then" as...
1
by: Phill. W | last post by:
I understand how to use the IExtenderProvider Interface to add a "dynamic" property to a Control, but is there any equivalent "mechanism" for using this extendability with just /any-old/ class,...
3
by: CoreyWhite | last post by:
I wrote a program that shows some incredibly curious probabilities. It is a simple game where you guess a number between 0 and 2 that the computer thinks up. What is origonal about this game is...
30
by: questions? | last post by:
say I have a structure which have an array inside. e.g. struct random_struct{ char name; int month; } if the array is not intialized by me, in a sense after I allocated a
4
by: Lance | last post by:
Hi all, I'm curious as to the technicalities of why a particular method declaring and calling is faster than the other. The C syntax is: \\\\\ BOOL PathMatchSpec( LPCSTR pszFile, LPCSTR...
1
by: Andrej Nerat | last post by:
Greetings, I was playing a bit with web services and asked myself if I could use P/Invoke to create AVI file in Web service from some images(bitmaps). I could already use P/Invoke to incorporate...
34
by: chandu | last post by:
Hello every body , upto now i saw advantages of C# only,i am curious about what are the disadvantages of C#..(because any programming language should have some positives and negatives..) Thanks...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.