ARTICLE

An assignable DateTime Class in VB.NET

Posted by Mike Gold Articles | Visual Basic 2010 January 11, 2005
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.
Download Files:
 
Reader Level:

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.

Dim dt1 As
DateTime = DateTime.Now
Dim dt2 As DateTime = New
DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.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.

Dim dt2 As DateTime = 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.

Private dt As
DateTimeExtended = DateTime.Now
Private dt.Hour = 6
' illegal, read-only property
Private 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 Property Hour() As
Integer
Get
Return
_val.Hour
End
Get
Set
' first subtract off existing hours
_val = _val.Subtract(New
TimeSpan(_val.Hour, 0, 0))
_val = _val.AddHours(Value)
End
Set
End
Property

Listing 4 gives you the idea of how I can propagate any existing methods or properties of the DateTime class and use them however I wish. Another example is propagating the powerful and formattable ToString method of the DateTime class shown in listing 5.

Listing 5 - Propogating the ToString method for formatting date time strings.

Public Overloads Function ToString(ByVal format As String) As
String
Return
_val.ToString(format)
End
Function

Since DateTimeExtended is simply a subset of DateTime, you can also create a static method that allows you to implicitly assign a DateTimeExtended object to a DateTime object. This way the compiler will automatically allow the conversion without the explicit (DateTime) prefix whenever you reference it to a DateTime type.

Listing 6 - Implicit conversion from DateTimeExtended to DateTime.

' implicitly conversion from DateTimeExtended to DateTime. returns
' the internal DateTime
'TODO: INSTANT VB TODO TASK: The following operator was converted, but you will need VB.NET 2005 to compile it:

Public Shared Widening Operator CType(ByVal x As DateTimeExtended) As
DateTime
Return
x._val
End
Operator

By utilizing a constructor of the DateTimeExtended class we can also do an implicit conversion in the other direction. Another words, we can assign a DateTimeExtended class to a DateTime class:

Listing 7 - Implicit conversion from DateTime to DateTimeExtended.

' Implicit Conversion from DateTime to DateTimeExtended
' creates a new DateTimeExtended and returns it
'TODO: INSTANT VB TODO TASK: The following operator was converted, but you will need VB.NET 2005 to compile it:

Public Shared Widening Sub CType(ByVal x As
DateTime)
Dim dt As DateTimeExtended = New
DateTimeExtended(x)
Return
dt
End
Sub

'constructor for constructing a DateTimeExtended
'class from a DateTime class

Public Sub New(ByVal dt As DateTime)
_val = dt
End
Sub

Implementing the DateTimeExtended Class.

Now we can do what we originally set out to do. We can create a DateTimeExtended class with today's date and assign it the time properties we wish. Below is a method demonstrating how to use the DateTimeExtended class to do just that. The method creates a new DateTimeExtended with today's date. It prints the original time and date out to the console. It assigns a new time to the DateTimeExtended object, and it writes out the new time to the console using the special datetime formatting capability.

Listing 8 - Demonstrating the DateTimeExtended class by assigning a new Time through its properties.

''' <summary>
''' Demonstrates using the DateTimeExtended class
''' </summary>

Public Sub
Show()
' Assign the DateTimeExtended class implicitly
' with today's date
Dim assignableDateTime As New
DateTimeExtended
' write the initial time to the console
Console.WriteLine("The time is {0}", assignableDateTime.ToString("MMM-dd h:mm"))
' assign a new time to hours and minutes
assignableDateTime.Hour = 6
assignableDateTime.Minute = 59
' write the new time to the console
Console.WriteLine("The time is now {0}", assignableDateTime.ToString("MMM-dd h:mm"))
End
Sub

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (
WWW.C-SHARPCORNER.COM).

share this article :
post comment
 
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Nevron Diagram
Become a Sponsor