CGI: VBScript Classes
VBScript and JavaScript CGI
VBScript Classes
VBScript does not implement full object orientation only the Encapsulation part of it. This alone is a very powerful feature and not difficult to learn. The following provides an introduction to encapsulation from a non-programming perspective. It’s aimed at scripting without all the techno babble.
Encapsulation
Encapsulation is nothing more than enclosing your code between Class and End Class tags. Code within these tags is in reality blueprint or plan for building a working piece of script code, each blueprint must have a unique name. A blueprint is turned into real working code using the New directive. To identify this new working code it is given a name using the assignment operator "=". A blue print can be used for creating any number working pieces of code each new code block must be giving a unique name
Although each new block of working code is identical they are totally independent of each other. Consider each block of code to be a box the only difference is the name printed on the box, this was assigned (printed on the box) using the New directive.
Class box_design |
Every blueprint Class must be given a unique name for example box_design. The blueprint consists of VBScript code. The End Class signifies we have completed the blueprint. | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
In the main script three boxes BOX1 to BOX3 are created (notice the unique names). The set directive creates a reference (name to be used) for each box. This is followed by the actual name for example BOX1. The box is created using the directive New and to use the name blueprint box_design. |
BOX1 |
BOX2 |
BOX3 |
Each box is unique (their names are different) however their content (script code) although currently empty are identical. |
Working ScriptThe above example has been put into a working script as shown on the right.
If you receive any errors these are generally caused by typos correct these. This working script provides a template allowing you to test code given in related topics covered on this page. |
'!c:/windows/system32/cscript //nologo <job> <script language="VBScript"> Wscript.Echo "Content-type: text/html"&vbLF&vbLF '--Class is just a template it can have any name Class box_design '...code... End Class '--Main Script code '--New creates the objects in this case boxes set BOX1 = New box_design set BOX2 = New box_design set BOX3 = New box_design </script> </job> |
Class overview
Classes are not only code containers they have the following features:
- Private and or public member variables.
- Access methods
- Private and or Public functions or subroutines
- A Constructor (runs when the new componet is created)
- A Destructor (runs when compont destroyed)
Variables and methods in a class are declared with either Private or Public access referred to as their scope.
- Private: Only elements inside a class can access elements defind as being Private. Nothing outside the class can access these.
- Public: Elements inside a class can access all elements including those defend as being Public. Any element defined as Public is accessible from outside using the dot operator.
Properties
Properties are nothing special they are standard variables with a defined scope of either Private or Public. They are restricted to containing only data; they cannot be arrays or classes.
Variables
Class box_design |
Properties (variables) in a Class must be defined with a scope of either Private or Public. Public variables are accessible from outside of the class. For example my_var1 | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
Main script creates three boxes BOX1 to BOX3 using the New directive and blueprint box_design. |
Using public variables is frowned upon! Always use private variables and allow access using the Let and Get directives. These allow you to create either write only or read only properties (variables) respectively.
Let and Get - Set and read variables respectively
Class box_design Public Property Let var1(sMsg) Public Property Get var1() '...code... |
Variable my_var1 has been changed from Pulic to Private. We are defining Properties these are made Public. Let and Get are directives. Each of these directives is followed by an Alias, name to be used externally. For example var1 Let Allows you to set a variable. sMsg is the string to be assigned to my_var1. Get Allows you to read a variable. The varial my_var1 is assed to var1 and returned. | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
Main script creates three boxes BOX1 to BOX3. |
Working Script: The above have been put into a working script as shown on the right.
If you receive any errors these are generally caused by typos correct these. Let
Public Property Let var1(sMsg) my_var1 = sMsg End Property Get
Public Property Get var1() var1 = my_var1 End Property |
'!c:/windows/system32/cscript //nologo <job> <script language="VBScript"> Wscript.Echo "Content-type: text/html"&vbLF&vbLF '--Class is just a template it can have any name Class box_design Private my_var1 'Private variables Private my_var2 'are properties Public Property Let var1(sMsg) 'Get sMsg and my_var1 = sMsg 'write to my_var1 End Property Public Property Get var1() 'Use Get to var1 = my_var1 'set var1 to my_var1 End Property 'and return its value End Class '--Main Script code. Create three objects set BOX1 = New box_design BOX1.var1 = "Test1" Wscript.Echo BOX1.var1 set BOX2 = New box_design BOX2.var1 = "Test2" Wscript.Echo BOX2.var1 set BOX3 = New box_design BOX2.var1 = "Test3" Wscript.Echo BOX2.var1 </script> </job> |
Methods
Methods are the real workhorse of any class; you can use a subroutine or a function. The difference being a function returns a value while a subroutine does not it just performs some task.
Sub - Performs some task does not return a value
Class box_design Public Property Let var1(sMsg) '--- Sub performs some task End Class |
A Sub performs some task but does not return a value | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
The sub is called using the dot operator |
Function - Performs some task returns a value
Class box_design Public Property Let var1(sMsg) '--- Function returns a value End Class |
A Function performs some task and returns a value. For example the Function Calculate | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
The function is called using the dot operator |
Class Subroutines
There are two very important inbuilt Class Subroutines Class_Initialize() and Class_Terminate().
Class_Initialize()
Class box_design Public Function Calculate(value) 'Test function Private Sub Class_Initialize() 'Runs when object is End Class |
Class_Initialize() This subroutine is where you initialise your class. It automatically runs when a new component (using the New operator) is created, being part of the construction process is referred as a constructor.If you omit the constructor a blank one is automatically inserted. The constructor sets the value for my_var1. It can perform other tasks such as run functions and create objects. Note: Spelling Class_Initialize() | ||
set BOX1 = New box_design |
set BOX2 = New box_design |
set BOX3 = New box_design |
In this example after each BOX is created the Calculate function is used to confirm correct operation. |
Class_Terminate()
Class box_design Private Sub Class_Terminate() End Class |
Class_Terminate() This special subroutine Class_Terminate() is called:
You put any code needed to tidy up in this subroutine. |
Summary
This introduction to classes provides enough detail for you to design your own classes. Classes are important because they provide a way to mask complexity and provide a simple user interface.
Two classes are covered in this tutorial CGI Property Class and File DB class they form the basis of any CGI script and can be considered essential components.
OK! The techno babble is difficult to avoid, its required terminology, shame that.
Where to next
Next page covers a CGI Property Class allowing you to access any CGI variable.