Dates and nothingness

Sounds like a metaphysical post, but it isn't. In working with an object for which I needed a last change date, I set the DateTime variable value to Nothing in the constructor.

Dim lastReset as DateTime = Nothing 

But when I check for IsNothing() later in the code the check always fails due to the fact that the Nothing value for the DateTime (a value type in .NET) is actually MinValue. To make sure that it wasn't Nothing I would avtually have to compare to MinValue:

If lastReset <> Date.Minvalue Then DoSomething() 
 
 

Well, I was sure that .NET 2.0 fixed this with Generics and I was right. This is what happens when you do to much designing and not enough coding, I guess. The new Nullable object type can be Of DateTime, so that this now works:

Dim lastReset as Nullable(Of DateTime) = Nothing 
'… 
If IsNothing(LastReset) Then DoSomething() 
 
 

From a pragmatic programming perspective, this isn't a big deal. From a solid code perspective it is a rather large deal. If I say Nothing, I want NOTHING in there. I don't want the MinValue in there. I want Nothing in there. Then I want to see if the value is still nothing later.

Comments are closed

Bill Sempf

Husband. Father. Pentester. Secure software composer. Brewer. Lockpicker. Ninja. Insurrectionist. Lumberjack. All words that have been used to describe me recently. I help people write more secure software.

PageList

profile for Bill Sempf on Stack Exchange, a network of free, community-driven Q&A sites

MonthList