The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Adding Emptiness to the DateTime class

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Eric Gunnerson

Posts: 1006
Nickname: ericgu
Registered: Aug, 2003

Eric Gunnerson is a program manager on the Visual C# team
Adding Emptiness to the DateTime class Posted: Mar 23, 2004 10:11 PM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Eric Gunnerson.
Original Post: Adding Emptiness to the DateTime class
Feed Title: Eric Gunnerson's C# Compendium
Feed URL: /msdnerror.htm?aspxerrorpath=/ericgu/Rss.aspx
Feed Description: Eric comments on C#, programming and dotnet in general, and the aerodynamic characteristics of the red-nosed flying squirrel of the Lesser Antilles
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Eric Gunnerson
Latest Posts From Eric Gunnerson's C# Compendium

Advertisement

I got an interesting email from a customer today, asking for my opinion on how to deal with the concept of “Empty” in relation to DateTime values. They had decided to use the DateTime.MinValue value as an indication that the DateTime was empty.

The two options they were consider were:

1) Call static functions to determine whether a DateTime is empty
2) Just compare the DateTime value to DateTime.MinValue to see if it is empty.

After a bit of reflection, I decided that I didn't like either approach. The problem is that they're trying to add a new concept of “emptiness” (some might equate this to “null”) to a type without changing the type.

A better approach is to define a new type that supports the concept of emptiness. In this case, we'll create a struct that encapsulates the DateTime value and lets us deal with empty in a more robust manner.

Here's the struct I wrote for them (and, no, this is not an indication that I'm now writing classes when people ask me to). 

public struct EmptyDateTime
{
    DateTime dateTime;
public EmptyDateTime(DateTime dateTime) { this.dateTime = dateTime; }
public bool IsEmpty { get { return dateTime == DateTime.MinValue; } }
public static explicit operator DateTime(EmptyDateTime emptyDateTime) { if (emptyDateTime.IsEmpty) throw new InvalidOperationException("DateTime is Empty"); return emptyDateTime.dateTime; }
public static implicit operator EmptyDateTime(DateTime dateTime) { return new EmptyDateTime(dateTime); }
public static EmptyDateTime Empty { get { return new EmptyDateTime(DateTime.MinValue); } } }

Read: Adding Emptiness to the DateTime class

Topic: The Visual History of the Graphical User Interface Previous Topic   Next Topic Topic: Tablet PC and Windows XP SP2 (RC1)

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use