Null

  • Thread starter Thread starter Jim Heavey
  • Start date Start date
J

Jim Heavey

Hello, If you have a method which test field to see if it is a valid date
and either returns a DateTime value which either has a valid datetime or it
has null, how can you test if the value returned is null? I tried and I
get an compile error "Can not be applied to operands System.DateTime and
null?

dtStart != null

I also tried the is operator

dtStart is null

but it does not like this either...

Thanks in advance for your assistance!!!!!!!!!!
 
DateTime is a value type :-(
In that sense, it cannot be set to null.
The easiest solution is to define a particular date that will act as a null
(eg: minval)

I remember reading a post about this refering a site where you can download
a class implementing DateTime feature.

José
 
Hi,

It doesn't work because DateTime is a structure which in C# is a value type
not a reference type so assigning null to it has no meaning. It's the same
as doing;

int x;
if(x != null) ;

John.
 
you cannot use null with DateTime but you can do

if (dtStart==DateTime.Empty())

not tested but my guess :)
 
codymanix said:
you cannot use null with DateTime but you can do

if (dtStart==DateTime.Empty())

not tested but my guess :)

There *is* no DateTime.Empty() method.
 
Back
Top