|
|||
Adding Public and Private Methods to a ClassHow to Customize VBScript Classes by Adding Bespoke MethodsMethods are a vital part of any class. They enable programmers to use the objects effectively, and for objects to communicate with each other.
The structures of all classes are pretty well the same regardless of the programming language being used. And really that's the whole point of them. A programmer needs know nothing about the internal workings of a class. Often they don't even need to know what programming language it was written in. All they need to know is:
The programmer can then go on and use this class in their own applications. And, for the new programmer, VBScript (or Visual Basic Script) is a particularly good place to start. That's because:
And all that's required is a text editor. Notepad will suffice, but there are editors designed for the job (and free) such as the Programmer's Notepad. Creating Classes and adding PropertiesThe first step is to create a class. The class will then need properties (the information about a class, such whether it has a color or legs). Both the creation of the class and addition of properties are covered in Starting Object Oriented Programming in VBScript: How to Create, Use and Reuse OOP Classes in a VBScript Application. Special Methods of a ClassAs well as properties a class will have special methods. These run automatically, for instance:
And they're discussed in How to Initialise and Terminate a VBScript Class: Creating VBScript Subroutines That Run When a Class is Instantiated. There are other special methods that are run when properties are updated or read which are discussed in Using a Property Let and Get in a VBScript Class: How to Work with an Object's Properties and VBScript Programming. Public and Private Methods of a ClassThe programmer can add their own customized methods to a VBScript Class. These methods can be one of two types:
These can be seen in operation in a simple class: Class Person
Public Name
Private Function format_greeting (ByRef new_person)
format_greeting = Name & " says ""Hello, " & new_person.Name & """"
End Function
Public Sub greet (ByRef new_person)
wscript.echo format_greeting (new_person)
End Sub
End Class
It is now possible to instantiate objects from this class and those objects can start to communicate with each other: Dim Person1: Set Person1 = New Person
Person1.Name = "Fred"
Dim Person2: Set Person2 = New Person
Person2.Name = "Jane"
Person1.Greet Person2
Person2.Greet Person1
The end result can be seen in figure 1 at the bottom of this article. They show just how easy it is to add private and public methods to class, and that not only can a programmer communicate with objects, but the objects can actually communicate with each other.
The copyright of the article Adding Public and Private Methods to a Class in Windows Programming is owned by Mark Alexander Bain. Permission to republish Adding Public and Private Methods to a Class in print or online must be granted by the author in writing.
|
|||
|
|
|||
|
|
|||