Adding Public and Private Methods to a Class

How to Customize VBScript Classes by Adding Bespoke Methods

Adding Public and Private Methods to a Class - Mark Alexander Bain
Adding Public and Private Methods to a Class - Mark Alexander Bain
Methods 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 properties of the class
  • the methods of of class

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:

  • it is very easy to create and use a class with VBScript
  • VBScript is built into every Windows computer

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 Properties

The 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 Class

As well as properties a class will have special methods. These run automatically, for instance:

  • when the object is instantiated
  • when the object is terminated

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 Class

The programmer can add their own customized methods to a VBScript Class. These methods can be one of two types:

  • private – these can only be used within the class
  • public – these are visible to the users of the class

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.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement