|
Tech Note 29: Working with ClassesAugust 19, 2006© NS BASIC Corporation. All rights reserved. |
VBScript supports the concept of objects and allows us to define classes and create instances, however it does not support the concepts of inheritance or polymorphism. Because of this rescriction, it is known as Object Based rather than fully Object Oriented. Despite this restriction, it is possible to simulate some level of inheritance using a technique called delegation which will be presented in the topics below.
Class Foo Private FooAttribute Public Sub FooSub() MsgBox "Foo.FooSub" End Sub Public Function FooFunction() MsgBox "Foo.FooFuncion" FooFunction = "value" End Function End ClassThis defines a new class called Foo with an attribute called FooAttribute. Since it was declared using the keyword Private, it will only be visible to the methods inside the class. If you want to make an attribute accessible outside the class, consider using the keyword Public. It is good programming practice to keep attributes as Private and use Properties to access them. This would allow the data to be checked before being set or read.
Dim objFoo Set objFoo = New FooThis creates an instance of the class declared in the previous section and assigns it to the objFoo variable.
Dim myValue objFoo.FooSub() myValue = objFoo.FooFunction() MsgBox "myValue = " & myValueThe two methods declared in the class definition are called, in the first case there is no return value(a Sub), in the second we assign the return to the variable myValue (a Function).
Class Foo Private i_FooAttribute Public Property Let FooAttribute(byVal Param1) ( ... code to check attribute value compliance ... ) i_FooAttribute = Param1 End Property Public Property Get FooAttribute ( ... code to format attribute output ... ) FooAttribute = i_FooAttribute End Property End Class
Class SubFoo Private parent Private Sub Class_Initialize() Set parent = New Foo End Sub Public Sub FooSub() parent.FooSub End Sub Public Function FooFunction() FooFunction = parent.FooFunction End Function Public Sub SubFooSub MsgBox "This is unique to the SubFoo class" End Sub End Class Dim objSubFoo, value Set objSubFoo = New SubFoo objSubFoo.FooSub value = objSubFoo.FooFunction objSubFoo.SubFooSub MsgBox "Value = " & CStr(value)The catch is the link made between the attribute parent and the "parent class". This is automatically done when an object of class SubFoo is instantiated, because of SubFoo's constructor, defined by the special private method Class_Initialize. The constructor and destructor concepts are shown below.
We can use a Function as a workaround to the parameters restrictions on Class_Initialize:
Function NewFoo(Param1, Param2, ...) Dim myFoo myFoo = New Foo myFoo.attr1 = Param1 myFoo.attr2 = Param2 ( ... additional initialization steps ... ) Set NewFoo = myFoo End Function
To have a method automatically called when and object is destructed, use the Class_Terminate() method:
Class Foo ( ... class code ... ) Sub Class_Terminate() ( ... cleanup code ... ) End Sub End Class
Despite the lack of support on inheritance and polymorphism, the rest of OO support is very stable and it will provide whole new world to write complex applications in NS Basic/CE.