Classes And Object Oriented Programming

Tips >> Visual Basic

MAKE WEB APPLICATION WITHOUT KNOWLEDGE OF CODING? CLICK HERE

Instead of breaking down a problem into smaller chunks as with procedural programming techniques, object-oriented programming is achieved by viewing the problem as a collection real-life Objects.

Objects are classified into classes using a process known as Abstraction. We concentrate on the data and behaviour of the objects. The behaviour is called the Operations of the class and the data is known as the Attributes or properties of the class. Each attribute and operation is said to be a member of the class. The class classifies the object and an object is created by making an instance of the class. When an instance of a class is made, the operations are called Methods.

CLASSES IN VISUAL BASIC:

Classes are added to the project by either right-clicking on the Project Name in the Project explorer and selecting Add and Class Module or from the Project menu and selecting Add Class Module. Unlike modules, classes are generally identified by a Capital C rather than a three-letter mnemonic at the start of the name.
For example a class for a clock might be called CClock which would be assigned to the Name property of the class.

To implement the attributes (properties) of a class in Visual Basic, the attribute name is preceded with the keyword Private and then the attribute name followed by the data type. The attribute name is normally preceded by a lowercase m meaning member (eg. mHour).

Being declared Private means that the attributes may not be accessed directly. The advantage of this is that it is easier to ensure that the values are valid (eg. safeguard against specifying an Hour of 786 for a clock). The operations of the class are written just like any other procedure of function.

The following is an example of a Class Clock called "CClock". The class has the data members mHour, mMinute, and mSecond as attributes, and the operations (things it can do) setTime and displayTime.

Option Explicit
' Attributes of the class
Private mHour As Integer
Private mMinute As Integer
Private mSecond As Integer
' Operations for the class
Public Sub setTime(ByVal h As Integer, ByVal m As Integer, ByVal s As Integer)
     ' Ensure the time is a valid time
     If h >= 0 And h < 24 Then
          mHour = h
     Else
          mHour = 0
     End If
     If m >= 0 And m < 60 Then
          mMinute = m
     Else
          mMinute = 0
     End If
     If s >= 0 And s < 60 Then
          mSecond = s
     Else
          mSecond = 0
     End If
End Sub
Public Function displayTime() As String
     displayTime = Format(mHour, "00") & ":" & _
     Format(mMinute, "00") & ":" & _
     Format(mSecond, "00")
End Function

 

Clients of the class may use the class without having to know how the class has been implemented. In order to use the CClock class, the only thing the clients need to know are its interface (in this case setTime and displayTime) and what parameters are required if any.

INSTANTIATING AN OBJECT:

An object is instantiated with the New keyword. This can either be done when the variable is declared, or using the Set statement.

The following declares a variable that is assigned an object reference.

Dim t As New CClock

 

The following declares a variable and is instantiated using the Set Statement.

Dim t As CClock
Set t = New CClock
Me.Print "Initial time is: " & t.displayTime()
t.setTime 19, 30, 15
Me.Print "After calling SetTime: " & t.displayTime()
Set t = Nothing

 

The Private data types may not be accessed directly, instead Properties are used for access if required.

AGGREGATION:

Classes may themselves contain other classes. This type of arrangement is known as Aggregation or Composition and promotes reuse, one of the major features of the Object Oriented approach. An example of Aggregation is used in the Properties example.

GENERALISATION:

Generalisation is a means of abstraction. More specific classes are Inherited from more general classes to define data and behaviour.

INTERFACE INHERITANCE:

Visual Basic 6 does not support Inheritance, but does support Interface Inheritance. Interface methods are usually empty shells with a code body to provide a Super class from which other classes may inherit the interface. Interface classes are prefixed with a capital I (eg. IShape). VB.Net does support Inheritance, as well as function overloading.

In the following example, an Interface Class called IShape defines the method calculateArea that may be used by shapes such as circle, rectangle, triangle, etc. The actual implementation of the calculateArea would be different for each shape, and therefore cannot be implemented. When a method behaves differently in derived classes, but essentially means the same thing, it is said to be Polymorphic.

Two classes are defined, CCircle and CRectangle which implement the Interface Class IShape. To state that they implement the interface class, the statement Implements IShape is added at the top of both classes. The classes then have to provide a definition for the abstract method IShape_calculateArea.

The IShape Interface Class:

Option Explicit
Public Function calculateArea() As Double
' Left empty
End Function

 

CCircle Class

The CCircle class implements the IShape class, and define the method, calculateArea().

Option Explicit
Implements IShape
Private mX As Integer
Private mY As Integer
Private mRadius As Integer
' Provide Interface definitions
Public Function IShape_calculateArea() As Double
     IShape_calculateArea = 3.14159 * mRadius ^ 2
End Function
' Add properties for this class
Public Property Get X() As Integer
     X = mX
End Property
Public Property Let X(ByVal xVal As Integer)
     mX = xVal
End Property
Public Property Get Y() As Integer
     Y = mY
End Property
Public Property Let Y(ByVal yVal As Integer)
     mY = yVal
End Property
Public Property Get Radius() As Integer
     Radius = mRadius
End Property
Public Property Let Radius(ByVal r As Integer)
     mRadius = r
End Property

 

CRectangle Class

The CRectangle class implements the IShape class, and define the method, calculateArea().

Option Explicit
Implements IShape
Private mX As Integer
Private mY As Integer
Private mWidth As Integer
Private mHeight As Integer
' Provide Interface definitions
Public Function IShape_calculateArea() As Double
     IShape_calculateArea = mWidth * mHeight
End Function
' Add properties for this class
Public Property Get X() As Integer
     X = mX
End Property
Public Property Let X(ByVal xVal As Integer)
     mX = xVal
End Property
Public Property Get Y() As Integer
     Y = mY
End Property
Public Property Let Y(ByVal yVal As Integer)
     mY = yVal
End Property
Public Property Get Width() As Integer
     Width = mWidth
End Property
Public Property Let Width(ByVal w As Integer)
     mWidth = w
End Property
Public Property Get Height() As Integer
     Height = mHeight
End Property
Public Property Let Height(ByVal h As Integer)
     mHeight = h
End Property

 

Using the Classes

The following Form uses both classes CCircle and CRectangle, and an interface reference to illustrate Polymorphism in Visual Basic.

draw.frm
Private Sub cmdDraw_Click()
Dim c As New CCircle
Dim r As New CRectangle
Dim interfaceRef As IShape
Set interfaceRef = c
c.Radius = 1000
c.X = Me.ScaleWidth / 2
c.Y = Me.ScaleHeight / 2
Me.Circle (c.X, c.Y), c.Radius
r.X = Me.ScaleWidth / 2 - 90
r.Y = Me.ScaleHeight / 2 - 90
r.Width = 180
r.Height = 180
Me.Line (r.X, r.Y)-(r.X + r.Width, r.Y + r.Height), , B
Set interfaceRef = c
picDisplay.Cls
picDisplay.Print "Area of circle: " & interfaceRef.calculateArea
Set interfaceRef = r
picDisplay.Print "Area of rectangle: " & interfaceRef.calculateArea
Set interfaceRef = Nothing
Set r = Nothing
Set c = Nothing
End Sub

 

If you don't find what you are looking for. Please click here to submit your query, our experts will reply soon.

 

E-mail : sales@virtualsplat.com
Phone : +91-9892413501

Whatsapp Icon +91-9967648641

Whatsapp Icon +91-9967648641