
Object-Oriented Development in VB
If you want to build any type of .NET application using VB, you are required to have a solid understanding of
OOP principles. ASP.NET web pages can only be constructed using OOP constructs. Desktop applications
(Windows Forms, WPF) demand using OOP constructs. Database access, XML web services, distributed
applications, and component interaction are all done using object-oriented techniques.
VB has been an “object-based” language for its entire existence. For example, all versions of VB have supported
the creation and use of intrinsic VB objects. VB4 introduced the class module but had no support for inheritance
or polymorphism. VB5 was a huge advancement in that it introduced interface-based polymorphism.
The .NET platform has granted full OOP abilities to VB. For all practical purposes, VB is as object-oriented as
C#, C++, or Java. Thus under .NET, VB programmers are able to build class hierarchies, overload operators,
establish abstract base classes, and more. To support all of these key OOP features, the current iteration of VB
includes many new keywords not found in earlier editions of the language.
The Three Pillars of Object-Oriented Programming
As a quick review, examine the three pillars of object-oriented programming:
• Encapsulation: hides implementation details.
• Inheritance: represents “is-a” relationships.
• Polymorphism: takes on many forms.
For the first time, VB fully supports all of these concepts. As you examine these concepts, you will see the
language constructs VB provides to support them. You will examine encapsulation first.
Encapsulation and Class Development
Of all the OO concepts, encapsulation is the easiest and one that VB has had since version 4. The idea is to take
data and behaviors and wrap them into one logical, physical entity. The details of the implementation are hidden
from the user of the object. Furthermore, users can only access and modify the state of the object by calling
special methods. This allows the object to protect itself from misuse. Encapsulation is all around us. Think
about your everyday DVD player. Do you know how to build one? Do you know all its internal workings? Yet
you probably use it all the time.
VB supports encapsulation via the Class keyword and other keywords that control visibility. When you create
a class, you are creating a custom type that you use to declare variables. An object is the actual bits and bytes of
this class in memory. Some refer to a class as a “blueprint” for objects. The blueprint analogy also applies to the
extent that you can and often do use a single class to create many objects.
The Class keyword is used to create a new class type. Private members of a class can only be used by the class
itself. Public members can be used from a class instance (a.k.a. an object) or the class itself. Here is a simple
class named Employee.
Public Class Employee
'Private fields. Only other Employee members can access these.
Private mName As String
Private mWage As Double
Private mHours As Double
' A single public function.
Public Function ComputePay() As Double
'We can use these fields because ComputePay is a member
'of the Employee Class.
Return mWage * mHours
End Function
End Class
VB provides the following access modifiers:
• By default, types are implicitly marked with the Friend keyword.
• By default, members of a type (constructor, field, method, and so on) are marked as Public.
• The Public modifier increases the visibility of a member or type.
• The Protected keyword will be examined later in the chapter.
Because a class is nothing more than a custom type, you use it to declare a variable of that type. Declaring a
class variable does not allocate the memory required to hold the fields. In OO terminology, it does not instantiate
the object. Instead it only allocates a small amount of memory, enough to hold a pointer or “reference,” in more .
NET-friendly terms. The reference begins life by referring to Nothing. Use the New keyword to allocate the
object.
'Declare the class variable.
Dim emp As Employee
'Create the object using the New keyword.
emp = New Employee()
'Or we could use these alternate syntax:
Dim emp2 As Employee = New Employee()
Dim emp3 As New Employee()
Object-Oriented Development in VB
Copyright (c) 2008. Intertech, Inc. All Rights Reserved. This information is to be used exclusively as an
online learning aid. Any attempts to copy, reproduce, or use for training is strictly prohibited.
OOP
Table of Contents
Courseware
Training Resources
Tutorials
Access Modifier
|
Meaning in Life
|
Public
|
Member can be accessed from anywhere.
|
Private
|
Member can only be accessed by other class members. This is the default visibility for members.
|
Protected
|
Member can be accessed by other class members and members of derived classes.
|
Friend
|
Member can be accessed from any type within the assembly but not from outside the assembly.
|
|