undefined value how?

  • Thread starter Thread starter TDOR
  • Start date Start date
T

TDOR

I am used to initilize vars with Null to mean undefined, but in c#
even complex types such as DateTime are considered value-types
so they cant be null. So far I have worked around the problem
using values that are outside the normal working range of my
programs or some other workaround, but in many places it turns
my code into an obscure piece of sh*t code! It make the code look
old fashion C, and not very nice to hand over to some other programmer
for maintainance. Anyone has a solution? - my books mention nothing of
this issue so its probably so simple it staring me stright in the face.
 
TDOR,

It's not a matter of DateTime being a complex type (which I don't know
what you mean by that), but rather, it is a value type, and value types can
not be null.

In the new version of .NET, there will be a structure introduced called
Nullable<T>, which is a generic. It will have a HasValue property, which if
true, means that the Value property returns a value. If it is false, then
the value is considered to be null.

Also, there will be new syntax in the language to handle this.
Basically, to have a nullable of any value type, you add ? after the type,
like so:

// Declare a nullable integer type.
int? pintNullableInt = null;

In this case, pintNullableInt.HasValue will return false. If you have
the community preview of VS.NET 2005, you can try it now.

Hope this helps.
 
Hi,

Teh only type I can think of with this problem is DateTime , but I'm sure
there are others, the solution is either wrap it on a custom type that does
support a "null" value or just assume that a value does that function,
DateTime.MinValue for example

Cheers,
 
We solved this problem in our app using a wrapper class. this way the date
could be null since the wrapper is a class.
 
Back
Top