Composition Example (Using generic approach to instance variables)

Option Explicit On ' Force explicit variable declaration.
Public Class clsEmployee
     Private mFirstName As String
     Private mLastName As String

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

          Public Sub New( )
               mDay = Microsoft.VisualBasic.DateAndTime.Day(Today)
               mMonth = Month(Today)
               mYear = Year(Today)
          End Sub

          Public Sub New (ByVal month As String, ByVal day As Integer, ByVal year As Integer)
               setDate (month, day, year)
          End Sub

          Public Function getDay( ) As Integer
               getDay = mDay
          End Function

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

          Public Function getMonth( ) As Integer
               getMonth = mMonth
          End Function

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

          Public Function getYear( ) As Integer
               getYear = mYear
          End Function

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

          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 ' clsDate

     Private mBirthDate As clsDate
     Private mHireDate As clsDate

     Public Sub New( )
          setFirstName("")
          setLastName("")
          mBirthDate = New clsDate( )
          mHireDate = New clsDate(
)
     End Sub

     Public Sub New(ByVal fName As String, ByVal lName As String, _
                             ByVal bMonth As String, ByVal bDay As Integer, ByVal bYear As Integer, _
                             ByVal hMonth As Integer, ByVal hDay As Integer, ByVal hYear As Integer)

          setFirstName(fName)
          setLastName(lName)
         
mBirthDate = New clsDate(bMonth, bDay, bYear)
          m
HireDate = New clsDate(hMonth, hDay, hYear)
     End Sub

     Public Function AsString( ) As String
          AsString = mLastName & ", " & mFirstName & _
                          " Hired: " & mHireDate.AsString( ) & _
                          " Birthday: " & mBirthDate.AsString( )
     End Function

     Public Function getFirstname( ) As String
          getFirstname = mFirstName
     End Function

     Public Sub setFirstname(ByVal fName As String)
          mFirstName = fName
     End Sub

     Public Function getLastName( ) As String
          getLastName = mLastName
     End Function

     Public Sub setLastName(ByVal name As String)
          mLastName = name
     End Sub

     Public Function getBirthDate( ) As String
          getBirthDate = mBirthDate.AsString( )
     End Function

     Public Sub setBirthDate(ByVal mth As Integer, _
                                   ByVal dy As Integer, _
                                   ByVal yr As Integer)
          Call mBirthDate.SetDate(mth, dy, yr)
     End Sub

     Public Function getHireDate( ) As String
          getHireDate = mHireDate.AsString( )
     End Function

     Public Sub setHireDate(ByVal mth As Integer, _
                                   ByVal dy As Integer, _
                                   ByVal yr As Integer)
          Call mHireDate.SetDate(mth, dy, yr)
     End Sub

     Protected Overrides Sub Finalize( )
          MyBase.Finalize( )
          mBirthDate = Nothing ' Release object's memory
          mHireDate = Nothing ' Release object's memory
     End Sub

End Class

Option Explicit On ' Force explicit variable declaration.

' Form module to test class clsDate and
clsEmployee 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     Dim employee1 As clsEmployee = New clsEmployee("Bob", "Jones", 9, 8, 1965, 2, 29, 2000)
     Dim employee2 As clsEmployee = New clsEmployee()

     employee2.setFirstname("Susan")
     employee2.setLastName("Baker")
     employee2.setHireDate(4, 21, 1994)

     txtOutput.Text &= employee1.AsString() & vbCrLf
     txtOutput.Text &= employee2.AsString() & vbCrLf
End Sub

Demo

Please Report Errors in Notes Here