Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Class strategy for handling null integers

Status
Not open for further replies.

JabbaTheNut

Programmer
Jul 29, 2002
176
US
I am looking for a good way to handle null integer values in a class. I need to develop a class that will extract data from several tables which can contain null values and provide the results in a set of property values.

For example...

##################################
private int x = (int) [database item which can be null];

public int Y
{
get
{
return x;
}
}
##################################

I understand that I can't convert a null value to integer (or set an integer variable to null). This being the case, what is the best strategy for this situation?

Game Over, Man!
 
Code:
if(Convert.IsDBNull(database item which can be null)){
  // you got NULL coming from the DB
}
 
To be totally correct, you should have a partner boolean value to indicate null-ness. But that's overkill for most applications, so I've been using int.MinValue to indicate a null.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
"int" is value type derived from class "object". It is not possible for any type to derive from a value type, all value types are implicitly sealed.
A variable of a value type , e.g. int always contains a value of that type.
It is not possible for a value of a value type to be null.
Assignment to a variable of a value type creates a copy of the value being assigned which differs from the assignment to a variable of a reference type.
The chiph suggestion is a solution for what you want but C# gives you all kinds of types and conversions.

-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top