OO Example*


 

*Using generic approach to instance variables


' Class clsDate definition

Public Class clsDate
     Private mMonth As Integer
     Private mDay As Integer
     Private mYear As Integer

     Public Sub New( )
          mDay = 1
          mMonth = 1
          mYear = 1900
     End Sub

     Public Function getDay( ) As Integer
          Return mDay
     End Function

     Public Sub setDay(ByVal dd As Integer)
          mDay = ValidateDay(mMonth, dd, mYear)
     End Sub

     Public Function getMonth( ) As Integer
          Return mMonth
     End Function

     Public Sub setMonth(ByVal mm As Integer)
          mMonth = ValidateMonth(mm)
     End Sub

     Public Function getYear( ) As Integer
          Return mYear
     End Function

     Public Sub setYear(ByVal yyyy As Integer)
          mYear = ValidateYear(yyyy) 
     End Sub

     Public Function AsString( ) As String
          Return mMonth & "/" & mDay & "/" & mYear
     End Function

     Public Sub SetDate(ByVal mth As Integer, _
                              ByVal dy As Integer, _
                              ByVal yr As Integer)
          mMonth = ValidateMonth(mth)
          mYear = ValidateYear(yr)
          mDay = ValidateDay(mMonth, dy, mYear)          
     End Sub

     Private Function ValidateMonth(ByVal mth As Integer) As Integer
          Return 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}
          Dim vDay as Integer

          If dy > 0 And dy <= daysPerMonth(mth) Then
               vDay = dy
          ElseIf mth = 2 And dy = 29 And isLeapYear(yr) Then
               vDay = dy
          Else
               ' An invalid day was passed to ValidateDay
               ' Set the day to a default value of 1
               vDay = 1
          End If
          Return vDay
     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
          Return year Mod 400 = 0 Or _
               year Mod 4 = 0 And _
               year Mod 100 <> 0
     End Function
End Class


Option Explicit On ' Force explicit variable declaration.

' Form module to test class clsDate  
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Dim testDate1 As clsDate = New clsDate ( )
     Dim testDate2 As clsDate = New clsDate ( )

     With testDate1 
          .setMonth(12)
          .setDay(25)
          .setYear(2003)
     End With

     testDate2.setDate(1, 1, 2004)

     txtOutput.Text &= testDate1.AsString( ) & vbCrLf
     txtOutput.Text &= testDate2.AsString( ) & vbCrLf
End Sub


Demo