1 Replies - 12351 Views - Last Post: 21 February 2012 - 08:21 PM

#1 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Handling a date property that isn't always set to a value.

Posted 21 February 2012 - 07:57 PM

I decided to put this here since I'm not really looking for help with code as much as I am just a discussion on your ideas.

I have a property called TerminationDate in my Employee Class that only gets set when the employees employment ends. The initialized property defaults to 01/01/0001 12:00:00 AM and I know I can check this prior to acting upon it but it's got me wondering if I'm missing something that isn't obvious to me. So my question is, how do you guys handle a date that you don't always set? Some of the ways I've played around with are:
  • Let the default save to the database and exclude it from any queries
  • Use a string instead of a date and parse it when datetime calculations are needed. (don't really like doing this)
  • Allow the date field to be null in the database (don't usually like storing null dates) and skip saving the date if it's the default.


So am I missing something simple that I'm not taking into consideration? I've thought about a second class that inherits the Employee class that is nothing more than this termination date. So more or less a TerminatedEmployee class. Is this a good practice?

Is This A Good Question/Topic? 0
  • +

Replies To: Handling a date property that isn't always set to a value.

#2 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Handling a date property that isn't always set to a value.

Posted 21 February 2012 - 08:21 PM

Albeit im sure you have already thought of this but what about:

public property TerminationDate() as Nullable(of Date)



public date? TerminationDate{get;set;}


This way you can store the value as a NULL in the database while still being able to check for Null.

Its been awhile but if i remember an initialized Date (or derivative) is set to basically the zero equivalent for the type of Date.

This post has been edited by woodjom: 21 February 2012 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1