Beruflich Dokumente
Kultur Dokumente
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.
Example:
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.
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:
GENERALISATION
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.
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
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