Object-Oriented Programming with VBScript (2)

Categories: Notes

Continuing previous post, I explore Object-Oriented Programming with VBScript.

Now I want to take a closer look at inheritance and practice its workaround, called delegation.

Delegator Class example

Class ScreenPoint
 '' Properties
 'Ancestor Point2D
 Private P2D
 'Point color
 Private Color
 '----------------------
 '' Methods
 'Constructor - called automatically
 Private Sub Class_Initialize()
 Set P2D = new Point2D
 End Sub
 '----------------------
 'Destructor - called automatically
 Private Sub Class_Terminate()
 Set P2D = Nothing
 End Sub
 '----------------------
 'A pair of methods to access private property X
 Property Get X
 X = P2D.X
 End Property
 Property Let X(ByVal in_X)
 P2D.X = in_X
 End Property
 '----------------------
 'A pair of methods to access private property Y
 Property Get Y
 Y = P2D.Y
 End Property
 Property Let Y(ByVal in_Y)
 P2D.Y = in_Y
 End Property
 '----------------------
End Class

As you can see, ScreenPoint class has Point2D property. When initialized, it refers to Point2D object. Note, that ScreenPoint class does not have X or Y properties, but Property Let / Property Get methods don’t have to be in sync with property declarations.

Here’s the example of using the new class.

Public Sub Test1()
 Dim SP
 Dim V1
 Set SP = new ScreenPoint
 V1 = SP.X
 SP.Y = 10
 Set SP = Nothing
End Sub

As you see, with Delegation we can effectively re-use data and code. The only inconvenience I found, is a boring amount of typing declaring Property Let / Property Get code blocks. Read next posts to see how I addressed it.


Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported
This work by the author is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported.