Dave Balzer

Confessions of a Webgypsy

<September 2008>
SuMoTuWeThFrSa
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011


Navigation

Blog Roll

Subscriptions

Post Categories



Thursday, June 16, 2005 - Posts

From Monkey to Master

This post has moved to www.DevAuthority.com

It can be found at http://devauthority.com/blogs/dbalzer/archive/2005/06/16/28.aspx

DevAuthority is a new blogging community welcoming new bloggers to join in.

posted Thursday, June 16, 2005 11:07 AM by dbalzer with 0 Comments

SqlDateTime Overflow Error Updated

Yesterday, I posted an article on a problem I was having with trying to insert a null value for a nullable datetime field in sql 2K.  Eric Spray sent me an email regarding this issue.  He said he couldn't post a comment (thanks to the problems we're having with the content spammers), but wanted to weigh in.  I really appreciate his comment and wanted to post it here.

From Eric:

The basic problem is: how to represent a null value when the type does not support null.  I have had this debated several times with colleagues, most use your solution by using a designated value to represent null (ie MinValue).  I don't like this solution, reason is you are using a valid value.  What if another developer or end user wants/needs to use that value, they are SOL.  Sometimes the requirements of a project do not allow you to use up a valid entry to represent null.  So, my solution is to have a property to manage the null state.  I like this solution better because it does not take away valid values.  Now lets say you need to restrict the values being entered (ie MyDate <= 1/1/2005).  I would still recommend you use the below pattern, that way WHEN the validation requirement changes it will be an easier change.

That's just my $.02

Here is my solution in C#
public class MyClass
   {
      private DateTime _myDate;
      private bool _myDateIsNull = true;//Initialize to true because the value is not set yet

      public DateTime MyDate
      {
         get
         {
            if(this.MyDateIsNull)
            {
               throw new NullReferenceException("MyDate is null!");
            }

            return _myDate;
         }
         set
         {
            //Input validation here
            if(value <= DateTime.Parse("1/1/2005"))
            {
               throw new ArgumentOutOfRangeException("Date must be greater than '1/1'2005'");
            }

            _myDate = value;
            _myDateIsNull = false;
         }
      }

      public bool MyDateIsNull
      {
         get
         {
            return _myDateIsNull;
         }
         set
         {
            _myDateIsNull = value;
         }
      }
   }

posted Thursday, June 16, 2005 10:19 AM by dbalzer with 0 Comments




Powered by Dot Net Junkies, by Telligent Systems