The Book of Visual Studio .NET - OOP in VB .NET Crash Course, Page 6
Adding Encapsulation
The encapsulation example implements the clsPerson class and encapsulated code, hiding the implementation code for all the properties and methods. In the abstraction example, the user has to enter both their birthday and age. As you encapsulate the implementation for the Person object, you will hide the implementation of their age. Age will be derived from the person's birth date, thus preventing a user from creating an invalid age and birth date values. Properties are also encapsulated, allowing the class to derive the full name from the first and last names that have been entered.
1. Add a new Windows Form item named "frmEncapsulation.vb".
2. Add the controls and parameters listed in Table 7- 5 to the frmEncapsulation.vb form.
|
Table 7-5: Controls for the Form Control Property Value Button Name btnSubmit Text Submit Textbox Name txtFName Textbox Name txtLName Label Name lblFullNameDisplay Textbox Name txtBirthDate Label Name lblAgeDisplay Textbox Name txtHoursWorked Label Name lblTotalHoursWorked |
3. Create a new class in the Person class using the following code:
Public Class clsPerson2 Private m_FName As String Private m_LName As String Private m_BirthDate As Date Private m_TotalHours As Integer Public Property FName() As String Get Return m_FName End Get Set(ByVal Value As String) m_FName = Value End Set End Property Public Property LName() As String Get Return m_LName End Get Set(ByVal Value As String) m_LName = Value End Set End Property Public ReadOnly Property FullName() As String Get Return m_FName & " " & m_LName End Get End Property Public Property BirthDate() As Date Get Return m_BirthDate End Get Set(ByVal Value As Date) If IsDate(Value) Then m_BirthDate = Value End If End Set End Property Public ReadOnly Property Age() As Integer Get If DatePart(DateInterval.Year, m_BirthDate) = 1 Then Exit Property End If Return DateDiff(DateInterval.Year, m_BirthDate, Now) End Get End Property 'Method for adding hours to m_TotalHours worked. Public Sub Work(ByVal intHours As Integer) m_TotalHours += intHours End Sub Public ReadOnly Property TotalHoursWorked() As Integer Get Return m_TotalHours End Get End Property End Class
4. Add the following object declaration to the initialization of frmAbstraction form:
' Here we are initializing the Person class. Normally this would be done ' when the class was needed for data access but in this case we are using ' the Person class to maintain our data. Dim objPerson As New PersonProj.clsPerson2()
5. Right- click and select Properties then change the StartUp object to "frmEncapsulation".
6. Add the following code to the Submit button:
objPerson.FName = txtFName.Text
objPerson.LName = txtLName.Text
If IsDate(txtBirthDate.Text) Then
objPerson.BirthDate = CDate(txtBirthDate.Text)
Else
MsgBox("Please provide a valid Birth Date.")
End If
If IsNumeric(txtHoursWorked.Text) Then
objPerson.Work(CInt(txtHoursWorked.Text))
txtHoursWorked.Text = ""
End If
lblFullNameDisplay.Text = objPerson.FullName.ToString
lblAgeDisplay.Text = objPerson.Age.ToString
lblTotalHoursWorked.Text = "Total Hours Worked: " _
& objPerson.TotalHoursWorked.ToString
7. Now run the application and enter the information.
You'll notice that your age is calculated for you so that it cannot contradict what the age should be based on the birth date, and the full name is derived from the first and last name.
