VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.00

"Class" Statement - Defining Your Own Class

This section provides a tutorial example on how to create a class and instantiate a new object. The TypeName() function returns the class name of the specified object.

Basic things that you need to learn before writing any classes:

1. "Class" Statement - Declares the name of a class and defines properties and methods that comprise the class. Here is the "Class" statement structure:

Class class_name
   ' statements to define properties
   ' statements to define methods
End Class

2. "New" Operator - Instantiates an object from the specified class. Here is the "New" operator syntax:

   Set variable_name = New class_name

3. "Set" Statement - Assigns an object to a variable. Here is the "Set" statement syntax:

   Set variable_name = object_expression

Now I am ready to show you that class is supported in the VBScript host environment provided by IE browser for client-side scripting. Here is my VBScript example that creates an empty class:

<html><body>
<!-- Empty_Class.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre><script language="vbscript">
   ' Initiating an object from a class
   Dim oEmpty
   Set oEmpty = New EmptyClass

   ' Checking the object
   document.writeln("VarType(oEmpty)=vbObject: " _ 
      & (VarType(oEmpty)=vbObject))
   document.writeln("TypeName(oEmpty): " & TypeName(oEmpty))

' Defining an empty class
Class EmptyClass
End Class
</script></pre>
</body></html>

Run this VBScript example in IE, you will get:

VarType(oEmpty)=vbObject: True
TypeName(oEmpty): EmptyClass

Note that "TypeNmae()" function returns the class name of the specified object.

Sections in This Chapter

Class, Property, Method and Related Statements

"Class" Statement - Defining Your Own Class

"New" Operator and "Nothing" Object

"Public/Private" Variables and Dot Operator

"Property Let/Set/Get" Procedures

Object Methods - "Public" Procedures

"New", "Set", "Is", ".", "Nothing" - Object Operations

"StringBuffer" - A Class Example

Dr. Herong Yang, updated in 2008
"Class" Statement - Defining Your Own Class