Thursday, April 7, 2011

VBScript/ASP User-Defined Class Objects





To define a user-defined Class Object, you use the Class statement to declare a class. The End Class statement defines the termination of the Class. Together, these statements form a Class construct, or Class block. E.g.

Class objName
‘ Place the Class variables, Properties and Methods here
End Class

In this syntax, objName is the name given to the Class Object. The class object name must follow standard VBScript variable naming conventions. Class Objects are usually declared in the variable definition sections. You can have multiple Class blocks in a single VBScript file, but each block must contain the Class …End Class statements. Classes cannot be nested.

Once you have defined the Class Object, you need to create an instance of the Class, similar to how other objects are created. When the Class Object is instantiated, memory is allocated for the Class Object. The Set statement is used with the New keyword to assign an instance of the class to a variable. With VBScript, this is the only time the New keyword is used (i.e. to instantiate a user-defined Class). E.g.

Dim MyObj
Set MyObj = New objName

The Object name MyObj is the Object variable name, and must follow standard VBScript variable naming conventions. The Object variable name is a reference (address) of the Object stored in memory, it is not the Object itself.

Inside the Class block, any Class variables, Properties, Methods and Events can be defined by the developer. The developer does not have to use all of the capabilities of the Class construct, i.e. Classes can be created without Methods or Properties. The design of the Class Object is completely up to the developer.

Class variables are created within the Class structure by using the Dim, Public, or Private statements. Variables defined within the Class structure by any of these statements must follow the standard VBScript variable naming conventions. Variables can be simple variables or arrays. E.g.

Class className
Dim var1, var2
Public var3, var4
Private var5, var6
End Class

The choice of the Dim, Public, or Private statements determine whether the variable is accessible outside of the Class Object. Variables are public by default, i.e. they are accessible outside of the Class Object. Both the Dim and the Public statements create public variables, while the Private statement creates variables that are not public. As a general rule, it is good programming practice to make all Class variables private, since the developer will want to tightly control when these variables are changed.
VBScript does not support Class-level Constants, i.e. named constants declared at the Class level. You cannot use the Const statement at the Class-level so that a constant can be used throughout a Class, but you can use the Const statement within a Property or Method. However, the constant will only have local scope within the Property or Method.



Class Object variables are accessible to VBScript code outside the Class through Class Properties. Class Properties “wrap” the Private variables of a Class. Inside the Class block, the Properties are defined by Property Get [|Let|Set] … End Property statement(s). For VBScript code outside the Class, the Property is accessed by referencing the Object Name.Property.

There are different types of Class Properties, depending on whether the Class variable is to be read, written to, or the Class variable is itself a Class Object. These Properties can be declared Public or Private.

Property Get
The Property Get procedure is used to access (return) private variables inside of the Class structure that are used as a read-only Property, or the read portion of a read-write Property. For VBScript code outside the Class, this type of Class Object Property is generally assigned to a variable or used in a conditional expression. The Property Get procedure returns a value to the calling code, and is general not used with any arguments. [Note: VBScript will let you add arguments to the Property Get procedure, but if you do so you must add the additional argument to the corresponding Property Let or Property Set procedure, since Property Let/Property Set must have one more argument than the corresponding Property Get procedure. It is generally considered bad programming form to have arguments in the Property Get procedure].

Property Let
The Property Let procedure is used to access (assign) private variables inside of the Class structure that are used as a write-only Property or are the write portion of a read-write Property. For VBScript code outside of the Class, this type of Class Object Property is usually assigned by a variable or a constant.

Property Set
The Property Set procedure is exclusively used when the Class Object needs to store
Properties that are object-based instead of numeric, date, boolean or string subtype variables. Property Set replaces the Property Let procedure. While Property Set and Property Let are functionally similar, there are two key differences:

  1. With the Property Set procedure, in the VBScript code segment (outside the Class block) you must use the syntax

Set Object1.Property = Object2

This is because VBScript does not let you use the assignment operator (=) to assign objects without the Set command.

  1. The Property Set procedure makes it clear that the Property is an object-based Property


Example:

Class FileSpec                     ‘ Define a Class block

Private master_file
Private master_FSO
Public Property Let FileName(strName) ‘ Define a Public Property to assign the file name
master_file = strName

End Property

Public Property Get FileName           ‘Define a Public Property to retrieve a file name

FileName = master_file
End Property
Public Property Set FSO(m_FSO)     ‘ Define a Public Property for an object
Set master_FSO = m_FSO

End Property

End Class

Rem Below is the VBScript code

Dim objFSO              ‘ Declare variables and objects
Dim objFilePointer, cur_file
Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFilePointer = New FileSpec                      
objFilePointer.FileName = “Myfile.mdb”                          
cur_file = objFilePointer.FileName             
Set objFilePointer.FSO = objFSO               ‘ Assigns an Object to the Property
Set objFilePointer = Nothing           ‘ Keyword Nothing releases the object memory


A couple notes on the example above. The CreateObject command is used to instantiate an Object that is known at the system level (e.g. a COM object). Also, so far this example only shows how to assign and retrieve property values. It is generally the Method(s) that control the action an object performs, not the properties.

A Property can be made read-only by only providing a Property Get procedure, or by declaring the Property Let procedure as Private instead of Public. A Property can be made write-only by only providing the Property Let procedure, or by declaring the Property Get procedure as Private instead of Public.

Class Methods are really just Functions and Subroutines inside of a Class block. These functions and subroutines can be either Private or Public. If they are public, they will be accessible to a VBScript code segment outside of the Class block by referencing the obj.Method. If they are private, they will only be available to code within the Class block.




An example of Class Methods is as follows:

Class FileSpec

Private master_file
Private master_FSO Private master_file
Private Sub Class_Initialize  ‘ Class Object initialization code
‘ code goes here

End Sub

Private Sub Class_Terminate  ‘ Class Object termination code

‘ code goes here

End Sub

Public Property Let FileName(strName) ‘ Define a Public Property to assign the file name

master_file = strName

End Property

Public Property Get FileName ‘ Define a Public Property to retrieve a file name

FileName = master_file
End Property
Public Property Set FSO(m_FSO)  ‘ Define a Public Property for an object
Set master_FSO = m_FSO
End Property
Public Sub Delete ‘Method to delete the master file
master_FSO.DeleteFile (master_file)
End Sub
End Class

Rem Below is the VBScript code

Dim objFSO ‘ Declare variables and objects
Dim objFilePointer, cur_file
Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objFilePointer = New FileSpec ‘ Instantiate the Class Object
objFilePointer.FileName = “Myfile.mdb”
cur_file = objFilePointer.FileName
Set objFilePointer.FSO = objFSO ‘ Assigns an Object to the Property
objFilePointer.Delete ‘ Executes a Method to delete a file
Set objFilePointer = Nothing ‘ Keyword Nothing releases the object memory




VBScript Class Objects automatically supports two type of Class Events; Class_Initialize and Class_Terminate Events. The code inside the Class_Initialize event executes once when an Object based on the Class is first instantiated. Any code put in this event is optional, and is typically used for initialization. Code inside the Class_Terminate event executes once just before the Object based on the
Class is destroyed (i.e. Set to Nothing, or the Object goes out of scope). Usage is as follows:

Class FileSpec

Private master_file
Private master_FSO Private master_file
Private Sub Class_Initialize ‘ Class Object initialization code
‘ code goes here

End Sub

Private Sub Class_Terminate ‘ Class Object termination code
‘ code goes here
End Sub

Public Property Let FileName(strName) ‘ Define a Public Property to assign the file name

master_file = strName

End Property

Public Property Get FileName ‘ Define a Public Property to retrieve a file name

FileName = master_file

End Property

Public Property Set FSO(m_FSO) ‘ Define a Public Property for an object

Set master_FSO = m_FSO

End Property

End Class

No comments:

Post a Comment