Introduction
Occasionally you run into a structure in the .NET library that you really wish was a class so you can inherit all its existing properties, override the ones you don't like, and add new properties and methods. Such a class is the DateTime class. Here I was sitting and writing some monitoring software and I wanted to re-assign a few of the properties of my DateTime class.
Let me give you the scenario.I wanted to create a DateTime with today's date and manipulate the time within the date. Seems simple enough, right? Here is what I found I needed to do.
Listing 1. Creating a time with today's date using two DateTime's
DateTime dt1 = DateTime.Now;
DateTime dt2 = new DateTime(dt1.Year, dt1.Month, dt1.Day, 9, 30, 0, 0);
So I needed two date times to complete my mission. I suppose I could have done the following
Listing 2. Creating a time with today's date using many DateTime.Now's
DateTime dt = new DateTime(DateTime.Now.Year,DateTime.Now.Month, DateTime.Now.Day, 9, 30, 0, 0);
But I suspect this an expensive call with all the DateTime.Now's in there. What would be ideal would be able to create a single DateTime class and assign it the hours, minutes, etc. that it needs. When I tried to assign values to Hours and Minutes properties, I soon found out that were read only.
Listing 3. Illegal version of assigning hours and minutes to a DateTime with today's date
DateTimeExtended dt = DateTime.Now;
dt.Hour = 6;// illegal, read-only property
dt.Minute = 59; // illegal, read-only property
In order to accomplish what I really wanted to do, I created a new class called DateTimeExtended. Because DateTime is a structure, the only way to glom the properties of DateTime is to create an aggregate member of the DateTimeExtended class that is a DateTime type. Then you need to propagate the DateTime properties up through the DateTimeExtended class.

Figure 1. UML Design of the DateTimeExtended class reverse engineered using WithClass
All I had to do now was override a few constructors and propagate the properties in which I'm interested. So how do I go about making Hour, Minute, and Second assignable? I want to leverage the DateTime object inside my DateTimeExtended class and encapsulate all the workarounds inside the class so as not to confuse the user. Below is the Hour property of the DateTime class. Note that I took advantage of the AddHours method to accomplish my goal of assigning the Hour property. This exact same technique is used for all the other DateTime properties (Year, Month, Day, Minute, Millisecond, etc.).
Listing 4. Propogating an assignable Hour property from the DateTime class
public int Hour
{
get
{
return _val.Hour; // just propogate the
// DateTime property here
// for the get
}
set
{
// the set is a bit more complicated.....
// first subtract off existing hours
_val = _val.Subtract(new TimeSpan(_val.Hour, 0, 0));
// then add back your assigned hours
_val = _val.AddHours(value);
}
}
Join the conversation! Your thoughts help the community grow.