In the new version of visual basic scripting, you can now create and
use classes. Any programmer should already know what a class is. Therefore
I will not explain what they are, only how to create and impliment them
in ASP and VBScript. This article is only a short intro. For more information,
please consult the VBscript 5.0 reference documentation available at:
http://msdn.microsoft.com/scripting/
To create a class, we use the class object. A class is created by typing
Class followed by the class
name. Then the class functions and closed by End
Class. You should get Something like this:
Class Fubar
'functions go here
End Class
|
Now inside the class you can have functions, sub-routines, and variable
declarations. Now lets create an example Function that just gives us a
message box sayign Hello!.
Class Fubar
Public Function myBox(strMsg)
msgbox strMsg
End Function
End Class
|
Now to implement the class:
Set myFubar = new Fubar
myFubar.myBox("Hello!")
|
Wow, pretty simple huh? Notice the Public
in front of the myBox function. What this
does is tell the script engine that the function is available to the user
outside of the actual class code. If we only wanted the function to be
used within the class code, we would put Private
in front of the function header. Lets take a look at this example:
Class Fubar
'***only code inside class can use message***
Private Function message(strMsg)
msgbox strMsg
End Function
'***you can access this function***
Public Function myBox(strMsg)
message(strMsg)
End Function
End Class
|
If you try and do the following, you will get an error:
Set myFubar = new Fubar
myFubar.message("Hello!") '<---can't do it
|
In ASP and VBScript, classes are formed exactly the same since asp uses
the vbscript scripting engine, so classes will work in both ASP 2.0 and
3.0. That's it for now. Questions, comments, wise cracks, please post
them in the forum.