I'm looking for a way to say that an integer doesn't have a legitimate value set. For example, in my database i've got some values that are null. When i pull these values out of the database i'd like to be able to set the Int32 Property to null or some equivelent
so I can run checks on whether or not the original value in the database was set to null or some number. Is there a way to set an integer to null or empty set or anything that says it's not set to an actual number?
::I'm looking for a way to say that an integer doesn't have a legitimate value set. One word: impossible. Int32 is a value type. Value tyes can not be null. Int32 makes no provisions to support his in any way, so it is not possible with an Int32. You need to
refer to another way (like wrapping the Int32 into another structure that has an additional bit to indicate whether ithas a value). ::Is there a way to set an integer to null or empty set or anything that says it's not set to an ::actual number? The combination
of: * The C# language specifications on structs and * The documentation of the Int32 struct amek it very clear that In32 has no provisions for this.
Why did they design C# this way? It seems to me to be a limitiation that I can't set an integer (or any other value type) to null or empty set. I probably can work around the limitation in my code, but then i'll have to know when -1 means an unset number versus
the valid -1. It makes my code less data driven and causes more coupling in my objects.
Why did they design C# this way? It seems to me to be a limitiation that I can't set an integer (or any other value type) to null or empty set. I probably can work around the limitation in my code, but then i'll have to know when -1 means an unset number versus
the valid -1. It makes my code less data driven and causes more coupling in my objects.
Use MinValue... Whenever I create an int, it gets set to MinValue (you have to instantiate it anyways, may as well be MinValue). If I need to check to see if it has been changed, I just check against minvalue.
int someInteger = int.MinValue;
.
.
.
if (someInteger == int.MinValue)
{
return "Oh crap, it's not set to anything";
}
You could always use an object and ensure checks for valid integers. For example....
object i = null;
// assign you object i with an integer value
i = 10;
if( i != null )
{
// Do what you have to with the value here
// I suggest that you check to make sure that the value is an integer
// Convert.ToInt32(i);
}
This way you can check for null values. Also, the performance hit isn't that significant, since essentially Int32 is an object. Get luck with your decision. :)
I still think that this is a limitation, but using MinValue does sound like a better work around then using zero. It would be less likely that MinValue would be confused with a valid number. Thanks for everybody's help.
>Why did they design C# this way? It seems to me to be a limitiation that I can't set an integer (or any other value type) to null or empty set. to understand why, you need to understand the fundamental difference between a value type and a reference type.
>This way you can check for null values. Also, the performance hit isn't that significant, since essentially Int32 is an object. no, Int32 is NOT essentially an object. there's a big difference in behavior. it's also quite significant when box and unboxing
value types.
aw232
Member
610 Points
120 Posts
Int32 i == null?
Mar 30, 2004 05:21 PM|LINK
thona
Member
20 Points
2923 Posts
Re: Int32 i == null?
Mar 30, 2004 05:40 PM|LINK
Sedgewick
Star
8400 Points
1680 Posts
Re: Int32 i == null?
Mar 30, 2004 06:15 PM|LINK
Charley Bogw...
Participant
1880 Points
374 Posts
Re: Int32 i == null?
Mar 31, 2004 07:45 PM|LINK
aw232
Member
610 Points
120 Posts
Re: Int32 i == null?
Mar 31, 2004 07:48 PM|LINK
Sedgewick
Star
8400 Points
1680 Posts
Re: Int32 i == null?
Mar 31, 2004 08:14 PM|LINK
Charley Bogw...
Participant
1880 Points
374 Posts
Re: Int32 i == null?
Mar 31, 2004 08:31 PM|LINK
Why did they design C# this way? It seems to me to be a limitiation that I can't set an integer (or any other value type) to null or empty set. I probably can work around the limitation in my code, but then i'll have to know when -1 means an unset number versus the valid -1. It makes my code less data driven and causes more coupling in my objects.
Use MinValue... Whenever I create an int, it gets set to MinValue (you have to instantiate it anyways, may as well be MinValue). If I need to check to see if it has been changed, I just check against minvalue.
int someInteger = int.MinValue; . . . if (someInteger == int.MinValue) { return "Oh crap, it's not set to anything"; }CraziJ
Member
355 Points
69 Posts
Re: Int32 i == null?
Mar 31, 2004 08:43 PM|LINK
object i = null; // assign you object i with an integer value i = 10; if( i != null ) { // Do what you have to with the value here // I suggest that you check to make sure that the value is an integer // Convert.ToInt32(i); }This way you can check for null values. Also, the performance hit isn't that significant, since essentially Int32 is an object. Get luck with your decision. :)Sr. Programmer
aw232
Member
610 Points
120 Posts
Re: Int32 i == null?
Mar 31, 2004 09:39 PM|LINK
shinakuma
Member
378 Points
92 Posts
Re: Int32 i == null?
Mar 31, 2004 09:43 PM|LINK