' Class
clsDateVB definition
Option Explicit On ' Force explicit variable declaration.
Public Class clsDateVB
Private mMonth As Integer
Private mDay As Integer
Private mYear As Integer
Public Sub New( )
mDay = 1
mMonth = 1
mYear = 1900
End Sub
Public Property Day( ) As Integer
Get
Day = mDay
End Get
Set(ByVal dy As Integer)
mDay = ValidateDay(mMonth, dy, mYear) ' note use of instance variables
End Set
End Property
Public Property
Month( ) As Integer
Get
Month = mMonth
End Get
Set(ByVal mth As Integer)
mMonth = ValidateMonth(mth)
End Set
End Property
Public Property
Year( ) As Integer
Get
Year =
ValidateYear(mYear)
End Get
Set(ByVal yr As Integer)
mYear = yr ' Could also be validated by programmer
End Set
End Property
Public Function AsString( ) As String
AsString = mMonth & "/" & mDay & "/" & mYear
End Function
Public Sub SetDate(ByVal mth As Integer, _
ByVal dy As Integer, _
ByVal yr As Integer)
mMonth =
ValidateMonth(mth)
mDay = ValidateDay(mMonth, dy, yr)
mYear =
ValidateYear(yr)
End Sub
Private Function ValidateMonth(ByVal mth As Integer) As Integer
ValidateMonth = IIf((mth > 0 And mth <= 12),
mth, 1)
End Function
Private Function ValidateDay(ByVal mth As Integer, _
ByVal dy As Integer, _
ByVal yr As Integer) As Integer
Dim daysPerMonth( ) As Integer = {0, 31, 28, 31, 30, 31, 30, 31, 31, _
30, 31, 30, 31}
If dy > 0 And dy <=
daysPerMonth(mth) Then
ValidateDay = dy
ElseIf mth = 2 And dy = 29 And
isLeapYear(yr) Then
ValidateDay = dy
Else
' An invalid day was passed to ValidateDay
' Set the day to a default value of 1
ValidateDay = 1
End If
End Function
Private Function ValidateYear(ByVal yyyy As Integer) As Integer
If yyyy > 0 Then
Return yyyy
Else
Return Year(Today)
End If
End Function
Private Function isLeapYear(ByVal year As Integer) As Boolean
isLeapYear = year Mod 400 = 0 Or _
year Mod 4 = 0 And _
year Mod 100 <> 0
End Function
End Class |