CGI: VBScript Classes

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
... code ...
End Class

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 Script

The above example has been put into a working script as shown on the right.

  • Save this to folder \www\vbs_test with name classes1.wsf
  • You can run this from the server by typing http://localhost:8081/vbs_test/classes1.wsf
    Browser will display a blank page.
  • Or by double clicking on the file name classes1.wsf
    This displays "Content-type: text/html" in a pop-up and nothing else.

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>

Top

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.

Top

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
   Public my_var1
   Private my_var2
   ... code ...
End Class

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
BOX1.my_var1 = "Test1"
Wscript.Echo BOX1.my_var1

set BOX2 = New box_design
BOX2.my_var1 = "Test2"
Wscript.Echo BOX2.my_var2

set BOX3 = New box_design
BOX3.my_var1 = "Test3"
Wscript.Echo BOX3.my_var1

Main script creates three boxes BOX1 to BOX3 using the New directive and blueprint box_design.
my_var1 set value example BOX1.my_var1 = "Test1"
my_var1 access value example BOX1.my_var1

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.

Top

Let and Get - Set and read variables respectively

Class box_design
  Private my_var1
  Private my_var2

  Public Property Let var1(sMsg)
   my_var1 = sMsg
  End Property

  Public Property Get var1()
   var1 = my_var1
  End Property

  '...code...
End Class

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
BOX1.var1 = "Test1"
Wscript.Echo BOX1.var1

set BOX2 = New box_design
BOX2.var1 = "Test2"
Wscript.Echo BOX2.var1

set BOX3 = New box_design
BOX3.var1 = "Test3"
Wscript.Echo BOX3.var1

Main script creates three boxes BOX1 to BOX3.
Note: The format used for setting and reading the variable my_var1 has changed. They now use the Alias name.

Top

Working Script:

The above have been put into a working script as shown on the right.

  • Save this to folder \www\vbs_test with name classes2.wsf
  • You can run this from the server by typing http://localhost:8081/vbs_test/classes2.wsf
    Displays Test1 to Test3 on a single line.
  • Or by double clicking on the file name classes2.wsf
    Displays "Content-type: text/html" in a pop-up.
    Followed by three more pop-ups displaying Test1 to Test3

If you receive any errors these are generally caused by typos correct these.

Let

  • Let writes to a private variable (Property)
  • Externally the Alias name is used. For example "var1"
Public Property Let var1(sMsg)
   my_var1 = sMsg
End Property

Get

  • Get reads a private variable (Property)
  • Externally the Alias name is used. For example "var1"
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>

Top

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
  Private my_var1
  Private my_var2

  Public Property Let var1(sMsg)
   my_var1 = sMsg
  End Property

  '--- Sub performs some task
  Public Sub List()    Wscript.Echo "An example of a Sub to perform <br />"
   Wscript.Echo "some task<br />"
   Wscript.Echo "Property my_ar1 = " & my_var1
  End Sub

End Class

A Sub performs some task but does not return a value
For example the Sub List prints out text and the property.

set BOX1 = New box_design
BOX1.var1 = "Test1"
BOX1.List

set BOX2 = New box_design
BOX2.var1 = "Test2"
BOX2.List

set BOX3 = New box_design
BOX3.var1 = "Test3"
BOX3.List

The sub is called using the dot operator
For example BOX3.List

Top

Function - Performs some task returns a value

Class box_design
  Private my_var1
  Private my_var2

  Public Property Let var1(sMsg)
   my_var1 = sMsg
  End Property

  '--- Function returns a value
  Public Function Calculate(value)
   new_value = value + my_var1 'Perform some calculation
   Calculate = new_value 'Return vew_value
  End Function

End Class

A Function performs some task and returns a value.

For example the Function Calculate
Adds the valued passd to the function to the property and returns the result.

set BOX1 = New box_design
BOX1.var1 = "1"
Wscript.Echo BOX1.Calculate(8)

set BOX2 = New box_design
BOX2.var1 = "2"
Wscript.Echo BOX2.Calculate(8)

set BOX3 = New box_design
BOX3.var1 = "3"
Wscript.Echo BOX3.Calculate(8)

The function is called using the dot operator
For example BOX2.Calculate(8) and returned value is echoed (output).

Top

Class Subroutines

There are two very important inbuilt Class Subroutines Class_Initialize() and Class_Terminate().

Class_Initialize()

Class box_design
  Private my_var1
  Private my_var2

  Public Function Calculate(value) 'Test function
   new_value = value + my_var1 'Perform some calculation
   Calculate = new_value 'returns vew_value
  End Function

  Private Sub Class_Initialize() 'Runs when object is
   my_var1 = 100 'created.
   '...Other code as required
  End Sub

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
Wscript.Echo BOX1.Calculate(8)

set BOX2 = New box_design
Wscript.Echo BOX2.Calculate(8)

set BOX3 = New box_design
Wscript.Echo BOX3.Calculate(8)

In this example after each BOX is created the Calculate function is used to confirm correct operation.

Top

Class_Terminate()

Class box_design

  Private Sub Class_Terminate()
  'Cleaning up code.
  'Release anything using RAM!
  End Sub

End Class

Class_Terminate()

This special subroutine Class_Terminate() is called:

  • When you set your object to nothing explicitly
  • Or when VBScript performs garbage collector killing it off.

You put any code needed to tidy up in this subroutine.

Top

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.


Top