Testing if Int has a value

J

John Smith

Hey folks,

I know this is an old topic, but I can't find a definitive answer on google.

How do I tell if an int has been initialized or not? I had been testing it
like:

if(myInt == Convert.ToInt32(null)){
:
}

But now I just realized that this is true:
Convert.ToInt32(null) == Convert.ToInt32(0)

and since myInt may contain 0 as a value, this won't work. So, how do you
do it? I'm starting to miss the simple good old days of
if(myInt == null)
 
N

Nicholas Paldino [.NET/C# MVP]

John,

Your int variables will ALWAYS have a value. By default, value types
have their bits set to zero, which in this case, results in the int variable
being 0.

In .NET 1.1, you would have to have a boolean flag indicating that the
integer was not initialized.

In .NET 2.0, there is a generic type called Nullable which is used for
exactly this situation. It should default to a value of null (HasValue is
false) by default, and when you set the value, HasValue should return true.

Hope this helps.
 
P

Paul E Collins

John Smith said:
How do I tell if an int has been initialized or not?
[...]
But now I just realized that this is true:
Convert.ToInt32(null) == Convert.ToInt32(0)

Int32 is a value type and cannot be null.

P.
 
S

Stoitcho Goutsev \(100\) [C# MVP]

Hi John,
and since myInt may contain 0 as a value, this won't work. So, how do you
do it? I'm starting to miss the simple good old days of
if(myInt == null)

Is it in Java? Because in most of the languages int cannot be tested for
null (in terms of pointers). Well with some exceptions ofcourse.

Anyways, If that *int* of yours is a local variable compiler is goign to
complain of using not not initialized local variable.
If it is a method parameter you may consider using *out* modifier to
generate compiler error.
If it is a member of a struct with constructor again the compiler is going
to complain.
The problem is when it is member of struct without constructor, static
member or member of class or array. Then you should take care of it by
yourself because the memeber is actually initialized and its initial value
is 0.

Other solution is to wait until .NET 2 and use nullable value types (which
are generics btw)
 
J

John Smith

See, my problem is I'm using it like this:

public int iChildrenCount{

get{

setValueFromDB(localiChildrenCount, colIndexToGet);

return Convert.ToInt32(localiChildrenCount);

}

set{

localiChildrenCount = value;

}

}

So, the compiler won't generate an error, and I won't know if the value
returned when I reference iChildrenCount is 0 or null.

Also, I have no way of removing whatever value is currently in
iChildrenCount. For example, iChildrenCount is a database field
corresponding to the question "How many children do you have?" and it allows
nulls in the database. The user can come back later and edit the form and
should have the option of not specifying a value or removing the answer they
previously specified. So, if they previously answered 3, and now they
remove that value, when I set iChildrenCount, I later won't know if the
answer to the question is 0 or not specified (at least not without
referencing the control on the form). In a case like that I can set not
specified to -1, but in other examples, -1 is a valid answer.

How do people ussually overcome this with out writing ugly code that does a
bunch of checks?
 
G

Guest

ahhh ... the evil Hungarian

for now, if -1 is not enough, use either Int32.MaxValue or Int32.MinValue.

2.0 will soon solve this nightmare.
 

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

Top