The Book of Visual Studio .NET - OOP in VB .NET Crash Course, Page 5
A class defines something that can exist in memory. It defines an object's interface including properties, methods, events, and enumerations as well as implementation code. An object is an instance of a class in memory; while a class may exist only once, multiple instances of that class may reside in memory as objects. When adding items to a project, you can add a "class" or a "component class". In essence, these are the same thing with one exception: a component class implements the IComponent interface, enabling Visual Studio .NET to drag non-visual controls, such as the timer control, onto a component designer. Visual Studio .NET provides a designer for building components, which allows you to drag visual controls onto your class or component, and begin coding. For example, if you want to program a delay into your class, you can use a component item with the designer to drag the timer control onto your component. The implementation of a graphical designer (IE Component Designer) and container are available to you when you selected "component class" as a new item. Visual Studio creates your class by adding a line of code that inherits everything your class needs to be a component, as follows:
Then the designer creates a components object using the IContainer interface so that the designer can allow drag and drop capabilities:
Private components As System.ComponentModel.Icontainer
Simple OOP Examples
1. Create a new class library project named "PersonProj".
2. Rename the Class1.vb file to "Person.vb".
3. Add a new Windows Application project to the solution named "TestClient".
4. Right- click on t he TestClient project and select Se t as StartUp Project.
Now add abstraction:
1. Replace the default class in Person.vb with the following code:
Public Class clsPerson Public FName As String Public LName As String Public FullName As String Public BirthDate As Date Public Age As Integer Public TotalHours As Integer Public Sub Work(ByVal intHours As Integer) TotalHours += intHours End Sub End Class
2. Create a form that looks like Figure 7-2, and name it "frmAbstraction.vb." Use the parameters in Table 7- 4 to build the new Windows Form.
Table 7-4: Form Parameters
Control Property Value
Button Name btnSubmit Text Submit Textbox Name txtFName Textbox Name txtLName Textbox Name txtFullName Textbox Name txtBirthDate Textbox Name txtAge Textbox Name txtHoursWorked Label Name lblTotalHoursWorked
3. Right- click on t he TestClient project and select Properties. Change the Startup object to "frmAbstraction.vb".
4. Add a r eference to the PersonProj. Right - click on t he TestClient project and select Add Reference. Select the Projects tab and press the "Select" button then press OK.
5. 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.clsPerson()
6. Add the following code to the submit buttons click event:
objPerson.FName = txtFName.Text objPerson.LName = txtLName.Text objPerson.FullName = txtFullName.Text If IsDate(txtBirthDate.Text) Then objPerson.BirthDate = CDate(txtBirthDate.Text) End If If IsNumeric(txtAge.Text) Then objPerson.Age = CInt(txtAge.Text) End If If IsNumeric(txtHoursWorked.Text) Then objPerson.Work(CInt(txtHoursWorked.Text)) End If lblTotalHoursWorked.Text = "Total Hours Worked: " _ & objPerson.TotalHours.ToString
7. Now run the application.
You should be able to place any value you wish into the First and Last name fields, then completely contradict yourself when filling in your full name. The same should hold true for entering your birth date and age. This example abstracts a person but does not hide any implementation; each time you press the Submit butt on, the Total Hours Worked is summed and displayed.
Page 5 of 7