null DateTime variables

F

Fred Nelson

Hi:

Another C# newby question:

How do I declare a null DateTime variable:

DateTime myDate = null;

I get the error:

"Cannot convert null to 'System.DateTime' because it is a value type"

(If I don't set a date then the value defaults to "1/1/0001")

I'm converting a VB.NET app to C# and I was able to declare null dates in
that language. Is there another Date type that will allow this - if not
then what is the best way to handle this - should I keep checking for 1/1/1
and consider that to be "null".

Thanks for your help!

Fred
 
C

Claudio Grazioli

On Thu, 19 May 2005 09:21:18 -0400, Fred Nelson wrote:

Hi Fred
How do I declare a null DateTime variable:

DateTime myDate = null;

I get the error:

"Cannot convert null to 'System.DateTime' because it is a value type"

(If I don't set a date then the value defaults to "1/1/0001")

I'm converting a VB.NET app to C# and I was able to declare null dates in
that language. Is there another Date type that will allow this - if not
then what is the best way to handle this - should I keep checking for 1/1/1
and consider that to be "null".

Well I don't know VB.NET really good, are you sure it is possible there?

Indeed as you already found out in C# that is not possible (it will be in
..NET 2.0 with nullable Types).

I'd say best way to do it is by treading DateTime.MinDate (what is 1/1/1)
as null value.

hth
 
F

Fred Nelson

Claudio:

Thanks for your help - I am comparing against DateTime.MinDate and it's
working just fine!

Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top