473,466 Members | 1,381 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Testing if Int has a value

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)
Nov 16 '05 #1
5 18912
"John Smith" <js@no.com> wrote:
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.
Nov 16 '05 #2
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"John Smith" <js@no.com> wrote in message
news:Ok****************@TK2MSFTNGP10.phx.gbl...
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)

Nov 16 '05 #3
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)
--
HTH
Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #4
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?

"John Smith" <js@no.com> wrote in message
news:Ok**************@TK2MSFTNGP10.phx.gbl...
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)

Nov 16 '05 #5
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.

"John Smith" wrote:
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?

"John Smith" <js@no.com> wrote in message
news:Ok**************@TK2MSFTNGP10.phx.gbl...
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)


Nov 16 '05 #6

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

Similar topics

1
by: Jim Hefferon | last post by:
Hello, Does anyone have suggestions for unit testing CGI scripts? For instance, how can I set the FieldStorage to have certain values? Do I have to go with a dummy FieldStorage class? And is...
0
by: David Bolen | last post by:
I ran into this strange behavior when noticing some missing spaces in some debugging output. It seems that somewhere in the print processing, there is special handling for string contents that...
2
by: Konstantin Zakharenko | last post by:
Hello, Our QA team have running a lot of test scripts (for automated regression testing), they run them on the different databases (Oracle/MS SQL). Several of those tests are dependent on the...
3
by: hendedav | last post by:
Hi gang. As with any other post, I am working on a project and have gotten stuck. I am trying to obtain a variable value in the parent webpage from an <iframe>. For instance: Parent Page code...
20
by: bhalicki | last post by:
Hi all, In the following code I am trying to change the contents of a string: int main() { char *string="testing"; rename(string); return 0;
11
by: Patrick.O.Ige | last post by:
When i try and use this (Where Unit is a column in my Table):- If Unit Is DBNull.Value Then Return "1" Else Return "2" End If I always have 2 returned! Even when Unit is NULL! I want a...
4
by: Jake Barnes | last post by:
I wanted to teach myself AJAX this weekend so I sat down with Stuart Landgridge's book and I started to play around. I came up with a little page that you can add text and images to. You can see it...
1
by: jobs | last post by:
If done something like this before: <asp:Button ID="Run" SkinID="Button" Text="Run" CommandArgument='< %#Eval("Jobname") %>' CommandName="Run" Enabled='<%# IIF(Container.DataItem("isrunning")=...
4
by: David | last post by:
Hi list. Do test-driven development or behaviour-driven development advocate how to do higher-level testing than unit testing? types of testing: unit integration system
5
by: dmorand | last post by:
I'm having a strange issue with a form I'm developing. I'm saving some values to hidden fields when a user clicks a button. I setup a function which gets ran on submission of the form, but I also...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.