Class Composition : Class Define « Class « VB.Net






Class Composition

Class Composition
Imports System

Public Class MainClass

    Shared Sub Main(ByVal args As String())
      Dim s As New CStudent( _
         "A", "B", 7, 24, 1949, 3, 12, 1988)

      Console.WriteLine(s.ToStandardString() )
      
    End Sub
End Class

' Encapsulates month, day and year.
Class CDay
   Inherits Object

   Private mMonth As Integer ' 1-12
   Private mDay As Integer ' 1-31 based on month
   Private mYear As Integer ' any year

   Public Sub New(ByVal monthValue As Integer, _
      ByVal dayValue As Integer, ByVal yearValue As Integer)

      mMonth = monthValue
      mYear = yearValue
      mDay = dayValue

   End Sub ' New

   ' create string containing month/day/year format
   Public Function ToStandardString() As String
      Return mMonth & "/" & mDay & "/" & mYear
   End Function

End Class 


' Represent student name, birthday and hire date.

Class CStudent
   Inherits Object

   Private mFirstName As String
   Private mLastName As String
   Private mBirthDate As CDay ' member object reference
   Private mHireDate As CDay ' member object reference

   ' CStudent constructor
   Public Sub New(ByVal firstNameValue As String, _
      ByVal lastNameValue As String, _
      ByVal birthMonthValue As Integer, _
      ByVal birthDayValue As Integer, _
      ByVal birthYearValue As Integer, _
      ByVal hireMonthValue As Integer, _
      ByVal hireDayValue As Integer, _
      ByVal hireYearValue As Integer)

      mFirstName = firstNameValue
      mLastName = lastNameValue

      ' create CDay instance for employee birthday
      mBirthDate = New CDay(birthMonthValue, birthDayValue, _
         birthYearValue)

      ' create CDay instance for employee hire date
      mHireDate = New CDay(hireMonthValue, hireDayValue, _
         hireYearValue)
   End Sub ' New

   ' return employee information as standard-format String
   Public Function ToStandardString() As String
      Return mLastName & ", " & mFirstName & " Hired: " _
         & mHireDate.ToStandardString() & " Birthday: " & _
         mBirthDate.ToStandardString()
   End Function ' ToStandardString

End Class ' CStudent

           
       








Related examples in the same category

1.Nested Class DemoNested Class Demo
2.Your Complex Number ClassYour Complex Number Class
3.Define and use your own Time ClassDefine and use your own Time Class