null == int testing

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

In VB you can test if a value type has the "default" value by doing the
following:

dim i as integer
if (Nothing = i) then
'* i is really 0
end if


Now, when I try to do this in C# of course it doesn't work because you can't
do:

int i;
if ( null == i) {
// i is 0
}

of course because you can't compare null == i.

Now, in this case I know, and you know, that an int defaults to 0, but there
are lots of types that default to some value that I don't know, e.g.
System.Drawing.Color.

Can anyone tell me how I can check if a value type is equal to its default
value?
 
You know you have an int, why don't you just set it to 0 when you create it?

int i = 0;

That way you know exactly what you're working with.

jeremiah();
 
to re-answer your question, since i misread it last time, i'll say that
the C# compiler, in every circumstance I have ever come across, will
always assign a sensible default value when you declare an integer.

if you have i declared at one point, and you mistakenly redeclare it,
you'll get a warning from the compiler.

just set all of your variables to some value that makes sense on creation.

also in C# i believe that you must do

if (i == null) {}

rather than

if (null == i) {}

as you have shown, but i'm not sure.
 
This may or may not help you now, but in C# 2.0 you can do

if (i == default(int)) ...

Also, your canned example -- even if altered to compare against 0, such as
"if (i == 0)" -- wouldn't even compile, because C# does not allow use of
uninitialized variables.

PS the idiom of putting the constant on the left-hand side of a comparison
is not needed in C#, because there is no implicit conversion to bool from
other types like there is in C/C++.
 
You can test if (i==new int()) or if (i==new MyStruct())

The default constructor of a value type will always return a
null-initialized struct.
 
This only works for value types that implement operator ==. It does not
compile in the general case.
 
also in C# i believe that you must do
if (i == null) {}

rather than

if (null == i) {}

as you have shown, but i'm not sure.


Either is perfectly acceptable, the reasoning for null == i is just no
longer there.
 
Ted Miller said:
This only works for value types that implement operator ==. It does not
compile in the general case.

True. Change it to:

if (i.Equals(new MyStruct())) and it works fine though.
 
Then we can use Object.Equals, which will suffer performance penalties
becaue of boxing

Best regards,
Sherif
 
<"Sherif El-Metainy" <elmeteny REMOVETHIS at thewayout NOSPAM dot
net> said:
Then we can use Object.Equals, which will suffer performance penalties
becaue of boxing

Using Object.Equals doesn't so much suffer performance penalties
because of boxing, but because the implementation of ValueType.Equals
uses reflection to compare the value of each field in turn.
 
I would have to check to be sure, but I don't believe that "if Nothing = i"
works in VB.NET either unless you have Option Strict Off. Which you never
should.

An int is a value type and as such can never equal null (or Nothing in VB
parlance).

In any case if it equals zero, then it does not and can not equal something
else, such as null.

This is the sort of muddled thinking that turns me off to VB. A default
value is not an unknown value and the two should never be confused.

--Bob
 
Bob Grommes said:
I would have to check to be sure, but I don't believe that "if Nothing = i"
works in VB.NET either unless you have Option Strict Off. Which you never
should.

Yes it does. Try this:

Option Strict On
Imports System

Public Class Test

Shared Sub Main()
Dim x as Integer = Nothing
Console.WriteLine(x)
End Sub
End Class

An int is a value type and as such can never equal null (or Nothing in VB
parlance).

Nothing is somewhat overloaded in VB.NET - it means the default value
for any type, so null, 0, false, '\0' or a struct with every field set
to the default value for its type.
 
I have to wonder why the default implementation doesn't do a bitwise binary
comparison of the struct like C's memcmp()? For reference types in the
default implementation only the reference is tested for bitwise equality,
and not the reflected fields are compared using Equals().
 
cody said:
I have to wonder why the default implementation doesn't do a bitwise binary
comparison of the struct like C's memcmp()? For reference types in the
default implementation only the reference is tested for bitwise equality,
and not the reflected fields are compared using Equals().

I think it does a bitwise binary comparison in some cases, but uses
Equals() to compare fields if any of the fields override Equals - so if
your struct contains a string reference, two distinct references to
equal strings in two instances of the struct won't stop those instances
from being equal.
 
Thanks everyone.

I was sure that I had tried:
if (new System.Drawing.Color() == MyColor)

But I must not have because it works like I wanted it to.

Thanks!
 
Yech. Thanks for the info. I have a VB.NET project to maintain and I'm
sure that sooner or later this "overloading" will rise up and bite me.
Especially if an empty string equals Nothing, too. I'll have to look into
that.

--Bob
 
On a realted note, I am having trouble withg the following. I wonder
if anyone can point out the fundamental, but totaly non-obvius prblem
here.
private bool EntryExists(string metaBasePath)
{
DirectoryEntry testedPath = new DirectoryEntry(metaBasePath");
if(new DirectoryEntry == testedPath) return false;
return true;
}

or even
private bool EntryExists(string metaBasePath)
{
DirectoryEntry shouldNotExist = new DirectoryEntry(@"IIS@//localhost/w3svc/1/Root/DoesNotExist");
DirectoryEntry testedPath = new DirectoryEntry(metaBasePath");
if(shouldNotExist == testedPath) return false;
return true;
}

According to reasoning elsewhere, the first of these should work, but
it doesn't. If you compare one non-existant DirectoryEntry to
another, the second should work. Again, however, it doesn't.
 
Marc Jennings said:
On a realted note, I am having trouble withg the following. I wonder
if anyone can point out the fundamental, but totaly non-obvius prblem
here.

Well, this code is syntactically incorrect, but I suspect that is not your
problem.

The problem here appears to be that DirectoryEntry does not override the ==
operator and therefore your comparison is simply comparing the references of
the two, meaning you will get false in both examples you gave.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top