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 -
PHP 5.3 Accelerates PHP    June 30, 2009
Sun Releases NetBeans 6.7 IDE for Java, PHP    June 29, 2009
Why Firefox Doesn't Take Google Chrome Features    June 26, 2009
First Major PHP Update in Years Coming Soon    June 25, 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  

Components

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:

Inherits System.ComponentModel.Component

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

Now you'll build an example application that employs abstraction, encapsulation, polymorphism, and inheritance:

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.

Adding Abstraction

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.

Creating a 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.

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