*$#&% Date

  • Thread starter Thread starter Jeff
  • Start date Start 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?
 
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
 
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.)
 
Best option is to set the date to System.DateTime.MinValue and check the
date against it.

Thanks,
Jagan Mohan
 
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
 
Back
Top