null date?

  • Thread starter Thread starter evolve
  • Start date Start date
E

evolve

why doesn't c# seem to support some kind of 'null date'

for instance:

i have an application where the user is not obliged to enter a date until
an event happens (e.g. a bug was fixed on xyz)

the calendar control in asp.net doesn't seem to have a selectedDate = null
property

is there any way around this?
 
Hi,

The DateTime data type is a value type and not reference
type.

For more information about value types and reference
types, look at these url's

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/csref/html/vcrefvaluetypes.asp

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/csref/html/vcrefvaluetypes.asp

The bottom line is, the value type cannot hold null
values unlike reference types.

So the work arounds which i can think of is to,

1) Assign a date like 1/1/1900 which will be considered
as null.
2) Create a class which has a field of type date. This
can be done in a smart way, such that you use it like a
normal value type, i.e., you don't need to use 'new' to
create the instance of the class.
This can be acheived by using implicit operator.
Thus, you get the ease of use of value type and also for
types like date you can assign null value since the class
is a reference type.

Hope this helps!!

Regards,
Madhu

MVP | MCSD.NET
 
Hiya,

This is a common challenge for developers with with the split
reference/value type system. Many developers go for a magic date which
"means" null but it is far from ideal. You may like to look at the
NullableTypes project being hosted at SourceForge. It provides wrapper
classes around the standard structs which are nullable. You will probably
want to see what impact these have on performance in your application - the
license terms seem to be OK.

----------------------------------------
- Mitch Denny
- (e-mail address removed)
- http://www.monash.net
- +61 (414) 610141
-
 
Use the static property DateTime.MinValue. (which is 01/01/0001 12:00AM)

This is considered to be a null date.

HTH
Brian w
 
There is one caveat to using DateTime.MinValue... the actual date
(01/01/0001) is not supported by most database vendors. For example, I
believe that SQL Server throws an exception for dates less than 1/1/1900.
You can write code to get around this by translating the DateTime.MinValue
to a DbNull value before writing it to the database but this is a little
troublesome unless you have a good framework setup. For an enterprise
application, you wouldn't want to rely on individual programmers to
constantly be checking against the DateTime.MinValue every time a database
access occurs. As some of the other responses pointed out, a wrapper class
is a good way to avoid some of these issues.

Hopes this helps....

Brian W said:
Use the static property DateTime.MinValue. (which is 01/01/0001 12:00AM)

This is considered to be a null date.

HTH
Brian w
 
Which is great, but they don't serialize...
-----Original Message-----
You could use System.Data.SqlTypes.SqlDate


"evolve" <NOngnSPAMpeaSPAMrseNO_S__PAMhotmail.com> wrote in message



.
 
Back
Top