Will "the Mighty" Strohl

Dynamically Creating a DataTable for Bindable Times

I came across a requirement where I needed to create a DataTable of times for a project.  This table didn't need to take up any space on the database since it would never change.  The times would need to be listed in 30 minute increments for each hour of the day.  This would then get binded mostly by dropdown lists.

Since the requirements said that it could (and should) be dynamically generated, I came up with the following methods for a utility class I have which allow me to do this.

Here is the example:

Public ReadOnly Property TimesKeyName as String
Return "timeskey"
End Get
End Property
Public ReadOnly Property TimesKeyValue as String
Return "timesvalue"
End Get
End Property
Public ReadOnly Property TimesAmPm as String
Return "timesampm"
End Get
End Property
Private _TimesTable As DataTable = Nothing
Public Property Times As DataTable
If Me._TimesTable Is Nothing Then
Me._TimesTable = Me.LoadTimesTable(Me._TimesTable)
End If
Return Me._TimesTable
End Get
Set(ByVal value As DataTable)
Me._TimesTable = value
End Set
End Property
Private Function LoadTimesTable(ByVal tblTable As DataTable) As DataTable
Dim row1, row2 As DataRow
tblTable = New DataTable("Times")
With tblTable
.Columns.Add(New DataColumn(Me.TimesKeyName, _
System.Type.GetType("System.String", False)))
.Columns.Add(New DataColumn(Me.TimesValueName, _
System.Type.GetType("System.String", False)))
.Columns.Add(New DataColumn(Me.TimesAmPm, _
System.Type.GetType("System.String", False)))
End With

' create local variables
Dim intI, intX As Integer
' load the sorted list with times
For intI = 1 To 24
' set an initial value
row1 = tblTable.NewRow
row2 = tblTable.NewRow
' add the time key/value & add the time to the row
If intI <= 12 Then
' morning hours
row1(0) = intI.ToString & ":00 AM"
row2(0) = intI.ToString & ":30 AM"
' add the 24 hour time (pad with a zero if needed)
intX = intI
row1(1) = _
Me.MakeSingleDigitHourDouble(intX.ToString) & "00"
row2(1) = _
Me.MakeSingleDigitHourDouble(intX.ToString) & "30"
' append the AM/PM values
row1(2) = "AM"
row2(2) = "AM"
' afternoon/evening hours
intX = intI - 12
row1(0) = intX.ToString & ":00 PM"
row2(0) = intX.ToString & ":30 PM"
' translate 24 hour time
row1(1) = intI.ToString & "00"
row2(1) = intI.ToString & "30"
' append the AM/PM values
row1(2) = "PM"
row2(2) = "PM"
End If
' add the row to the table
With tblTable.Rows
End With
Return tblTable
End Function
Private Function MakeSingleDigitHourDouble(ByVal _Hour As String) As String
' check to see if the variable is already two numbers
Dim objRegex As New Regex("^[0-9]{2}?")
Dim result As Match = objRegex.Match(_Hour)
If result.Success Then
' if it is two numbers, then return it
Return _Hour
' make sure it is a number
objRegex = New Regex("^[0-9]{1}?")
result = objRegex.Match(_Hour)
If result.Success Then
' it is a number
Return "0" & _Hour
' it is not - throw an error
' (that'll teach 'em!)
Throw New Exception("The _Hour variable must be a " & _
"number (String).")
End If
End If
End Function

There wasn't anything fancy done here.  I just looped to populate a DataTable with three columns.  One each for the key and value pair, and an additional column to aid in sorting and other helper functions elsewhere without having to parse the time string.

I am sure I will need to streamline this later on, but here is the first draft of this working and functional code.

blog comments powered by Disqus