Custom Class Objects in VBScript

A code class in VBScript (and most other programming languages) is a structure that houses a set of related properties, methods, and events that can be reused throughout your code. In object-oriented languages, this code class is instantiated as an object reference. In today’s article, I’m going to show you how you can create your own custom VBScript object classes for use in WSH, ASP, or HTML applications.

Contributed by
Rating: 3 stars3 stars3 stars3 stars3 stars / 5
March 16, 2009
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

In VBScript we’re used to using several different objects.  The WScript object, the Server object, and the FileSystemObject are just a few that you probably use every day.  These objects actually point to code classes.  In this case, we’re referring to classes that are housed inside of a dll file somewhere on the system and made available through the Component Object Model (COM).

The terms class, object, and component are sometimes, albeit incorrectly, used interchangeably.  An object is a reference to either a class or a component.  A class is a local structure that only exists at the time of execution.  It is only available to those scripts and programs that can access it directly.  It needs to be present in the current script.

A component, on the other hand, is a code package.  This package is stored globally on the system and can be reused by any script or program with permission to access it.  These components may contain one or more code classes that become available to the local script when the component is instantiated.  Most often, these components are pre-registered with the Component Object Model so that they can be referenced globally via their ProgID.  In this way, the system itself manages the connection to the component and the programmer does not need to know where the component file is located on the system.

In VBScript, components are instantiated using the CreateObject or GetObject methods.  Classes, on the other hand, are instantiated using the New keyword.  In each of these cases, an object reference is returned that can be assigned to a variable by using the Set keyword.  You should be used to seeing commands like the following.

Set objFso = CreateObject("Scripting.FileSystemObject")

Set objRe = New RegExp

The first is a typical use of the CreateObject method to connect to a COM component.  The latter demonstrates the use of the New keyword for instantiating a local class named RegExp.  RegExp is one of the few local classes provided by the scripting engine itself.

To date you have probably only used the default classes and components that ship with the Windows Scripting Host or with COM, but there are a plethora of others out there that you can install and use.  For now though, my next few articles will concentrate on creating your own classes and components.

Creating Code Classes in VBScript

Before we begin creating actual classes, let’s take a moment to think about when they should be used.  Classes should contain groups of related properties, methods, and events.

For instance, we could create a code class that is used to find information about cars in our inventory.  The class could return a Car object.  This object would most likely have some properties that set or return information about the car, such as Color, Size, Make, or Model.  It might also have methods that refer to actions performed on or by the car, such as StartEngine, EngageBrake, or BeginSale.  Finally, the Car object could also expose methods that notify our script or program when certain events happen to the car.  These might be EngineStarted, ScheduledMaintenance, or SaleCompleted.

As you can see, all of the functions that would be needed by a sales lot could easily be packaged into a single code class.  This allows the code to be easily ported from one application to another, and also allows for a single point of update, which makes managing future upgrades more feasible.  Aside from that, VBScript is an object-oriented programming language, so it only makes good sense to take advantage of that coding style.

A code class in VBScript is quite simply a group of functions enclosed in a Class structure.  The scripting engine handles the rest for you.  Today we’re going to be creating a class that can create Compressed Folders natively in WSH.  So to begin, we’ll create a class named CompressedFolder.

Class CompressedFolder

 

End Class

Voilà!  We now have a code class.  When the scripting engine parses this bit of code, it will create an object reference for our new class.  We can connect to that in your script using the following syntax.

Set objZipFolder = New CompressedFolder

Of course, at this point our class doesn’t actually do anything.  We need to add the relevant code for it to be useful.  It’s important to note that, with the exception of variable declarations, all code in a class must be procedural.  In other words, all code inside of your class must be contained in either a Function, Sub, or Property block.

Adding properties to a class

You can set the permission level on properties and methods inside of a class.  For our purposes, we’ll be setting them as either Private or Public.  Private properties and methods are only available to code within the class itself.  Public properties and methods are available to outside scripts and applications, and are exposed through the class object.

Properties and Methods are exposed through an object Interface.  With VBScript classes, these interfaces are created automatically for you.

There are two types of properties in VBScript: Read/Write and Read-only.  A read/write property allows you to retrieve the property's current value or assign one.  A read-only property will only allow you to retrieve the current value.

Properties are created using a Property structure.  There are three types of property structures: Property Get, Property Let, and Property Set.  Property Get is used to create a class property.  This is the structure that will Get or return the current property value.  Property Let is the counterpart; it accepts a parameter and allows the programmer to pass a value to be assigned to the property.  The Property Set structure is the same as Property Let, but it is used when a Property is being assigned an object value.

All properties must have a Property Get block.  Properties that only have a Property Get block are treated as Read-only.  Properties that add either a Property Let or Property Set block are treated as Read/Write.

A property is only required to have a Property Get structure.  Property Let and Property Set can be omitted if the property is to be treated as read-only.  You may also want to consider the fact that properties do not need to be made Public.  Private properties would only be available from inside the class itself.  I can’t think of a reason to do this myself, but I thought I’d at least mention it.

Class CompressedFolder

   Private m_fileName

 

   Public Property Get FullName

       FullName = m_fileName  

   End Property

 

   Public Property Let FullName(strName)

       m_fileName = strName

   End Property

 

   Public Property Get Path

       Path = Left(FullName, InStrRev(FullName, ""))

   End Property

End Class

More on adding properties

I’ve begun constructing my class by adding some properties.  Notice how the Property structures look like functions that are executed when a property is retrieved or assigned.  Immediately inside of my class I’ve created a few private variables.  These are global variables that I’ll be using throughout my class to track the value of the properties.

In this example, I’ve added two properties.  The FullName property has both Get and a Let, so it is a Read/Write property.  The Path property only has a Get structure, so it is a Read-only property.  These properties could now be used in my script.

Set objZipFolder = New CompressedFolder

WScript.Echo objZipFolder.FullName

objZipFolder.FullName = "My new FullName value"

WScript.Echo objZipFolder.Path

Here is the complete property listing for the CompressedFolder class.

Class CompressedFolder

   Private m_fileName

 

   Public Property Get FullName

       FullName = m_fileName  

   End Property

 

   Private Property Let FullName(strName)

       m_fileName = strName

   End Property

 

   Public Property Get Filename

       Filename = Right(FullName, Len(FullName) - InStrRev(FullName, ""))

   End Property

 

   Public Property Get Path

       Path = Left(FullName, InStrRev(FullName, ""))

   End Property

 

   Public Property Get Count

       Count = GetItemCount(FullName)

   End Property

End Class

Having a few properties doesn’t do much for this class.  We need a way to create the compressed folder and to manipulate it.  For that we need to add some methods.  These are the functions and subroutines that perform the magic when the class is called.  For that, you’ll also need to wait for part two of this series!  Until next time, keep coding!

blog comments powered by Disqus
CODE EXAMPLES ARTICLES

- Bipartite Graphs
- Connectivity in Graphs
- The Ford-Fulkerson Algorithm
- Critical Paths
- The Bellman-Ford and Roy-Floyd Algorithms
- Shortest Path Algorithms in Graphs
- Minimum Spanning Tree
- Articulation Edges and Vertexes
- Circles and Connectivity in Graphs
- Depth-First Search in Graphs
- Breadth-First Search in Graphs
- The Prufer Code and the Floyd-Warshall Algor...
- An Insight into Graphs
- Coding a Custom Object with WSC
- Creating a Custom Object with WSC

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials