developer.com
Search EarthWeb
CodeGuru | Gamelan | Jars | Wireless | Discussions
Navigate developer.com
Architecture & Design  
Database  
Java
Languages & Tools
Microsoft & .NET
Open Source  
Project Management  
Security  
Techniques  
Voice  
Web Services  
Wireless/Mobile
XML  
New
 
Technology Jobs  

   Developer.com Webcasts:
  The Impact of Coding Standards and Code Reviews

  Project Management for the Developer

  Defining Your Own Software Development Methodology

  more Webcasts...




See The Winners!




Developer Jobs

Be a Commerce Partner














 


Developer News -
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 2009
Red Hat CEO Calls on Oracle to Keep Java Open    June 25, 2009
Google Widens AdSense for iPhone, Android Apps    June 24, 2009
Free Tech Newsletter -

The Book of Visual Studio .NET - OOP in VB .NET Crash Course

Go to page: Prev  1  2  3  4  5  6  7  Next  

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.

Go to page: Prev  1  2  3  4  5  6  7  Next  

Previous article: The Book of Visual Studio .NET - Structured Exception Handling in VB


Tools:
Add www.developer.com to your favorites
Add www.developer.com to your browser search box
IE 7 | Firefox 2.0 | Firefox 1.5.x
Receive news via our XML/RSS feed


Visual Basic Archives






internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs