*$#&% Date

J

Jeff

I find dates to be a pain to work with in .Net. The main
problem is that if you set the date to Nothing it is not
Nothing. Does anyone have a good solution for testing if
a date parameter to a Sub is Nothing?
 
N

Nick Wienholt

The main
problem is that if you set the date to Nothing it is not
Nothing.

System.DateTime is a value type, so it can't be set to
nothing.
Does anyone have a good solution for testing if
a date parameter to a Sub is Nothing?

There are a couple of options:

1. Use the nullable types in System.Data.SqlTypes.
2. Use a third-party library like Nullable Types. (I
think this project lives on CodeProject).
3. Use a "special" DateTime like 1 Jan 1900 to signify
nullability.


Nick Wienholt, MVP
Maximizing .NET Performance
http://www.apress.com/book/bookDisplay.html?bID=217
Sydney Deep .NET User Group www.sdnug.org
 
R

Robert Jacobson

What about just using the default value ("00:00:00.0000000, January 1,
0001", or 0 ticks) as the "special" type? It seems a bit more
understandable then using a special "magic number" for the value.

Dim dt as DateTime
...

If dt.Ticks() = 0 then
... (it's not initialized)
Else
' Reset dt to zero (default value)
dt = New System.DateTime
End If

(Air code.)
 
J

Jagan Mohan

Best option is to set the date to System.DateTime.MinValue and check the
date against it.

Thanks,
Jagan Mohan
 
N

Nick Wienholt

Yep - that will do. This is still just another mutually agreed upon date
that is read as null while still being a valid date. Probably a better
choice that 1 Jan 1900.

Nick
 

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

Top