To read the article online, visit http://www.4GuysFromRolla.com/webtech/050102-1.shtml

Object Encapsulation In VBScript

By Francis Chau


Introduction
In an earlier article I wrote, Simulating Server.Execute and Server.Transfer in ASP 2.0, we examined a technique that used VBScript's Execute statement to dynamically execute VBScript code that was contained in a separate file. In this article I'll show you how to use the VBScript Execute and Eval statements to encapsulate and expose methods and properties of other objects in your own VBScript class objects. (For an introduction to these two statements, check out: Using the Eval and Execute Functions.) This will enable you to provide all the functionality of some object (e.g. 3rd-party or in-house component, another VBScript class object) in your own object. (For some examples of encapsulation in VBScript, check out: Use Classes to Encapsulate Implementation Complexity)

The Pass-Through Technique
I call this technique a "pass-through" technique. Essentially, property and method calls to the target object (the object you're encapsulating) "pass-through" your object and are executed on or evaluated by the target object. Ok, I don't think I've ever used the word "object" that many times in one sentence before! To clear things up, let's look at a demonstration in which I'll encapsulate the ASP Response and Request object, but this could work with any object. Here's a VBScript class object that encapsulates the ASP Response and Request objects:

Class PTWrapper
  Public Function PTMethodExecute(sMethodStatement)
    Dim sMethod
    sMethod = Replace(sMethodStatement,"\#", Chr(34))
    Execute "Response." & sMethod
  End Function

  Public Function PTMethodEval(sMethodStatement)
    Dim sMethod
    sMethod = Replace(sMethodStatement,"\#", Chr(34))
    PTMethodEval =   Eval( "Request." & sMethod)
  End Function
End Class 'PTWrapper

In this code segment, we have a VBScript class called PTWrapper with two functions: PTMethodExecute and PTMethodEval. The PTMethodExecute acts as a wrapper to the ASP Response object while the PTMethodEval acts as a wrapper to the ASP Request object. (For more on VBScript classes read: Using Classes within VBScript). In this PTMethodExecute function, we are expecting a string to be passed that will be executed by the VBScript Execute statement. Before we do that, we do a Replace() on the incoming string to replace \# with real quotation marks (") using Chr(34) to return a quotation mark. The \# is used as a place marker to simplify the construction of our execution string when we use our PTMethodExecute.

Here's some code that uses our PTWrapper object:

'Create an instance of the PTWrapper class
Dim oPTWrapper
Set oPTWrapper = new PTWrapper

oPTWrapper.PTMethodExecute ("Write \#Follow the white rabbit.\#")
oPTWrapper.PTMethodExecute ("Cookies (\#Username\#) = \#Neo\#")

Here we create an instance of the PTWrapper and call the PTMethodExecute twice. The first call to PTMethodExecute is to write a string to the output stream. The second call to PTMethodExecute stores the value "Neo" in a cookie variable called Username. This demonstrates the feasibility of run-time code evaluation or the capability to create code on the fly.

The function PTMethodEval is similar to the PTMethodExecute, however, in place of Execute we use the VBScript Eval statement to evaluate the constructed expression. The usage of Eval vs. Execute allows us to send a return value back to where the PTMethodEval was called. To demonstrate this, we can use the following:

Response.Write "<BR>Username = " & oPTWrapper.PTMethodEval("Cookies(\#Username\#)")

We use the oPTWrapper.PTMethodEval to evaluate a string that will return the stored value in our cookie variable Username. (Remember, we substitute \# in places where we normally would use double quotes.) Putting all this together we have this:

Class PTWrapper
  Public Function PTMethodExecute(sMethodStatement)
    Dim sMethod
    sMethod = Replace(sMethodStatement,"\#", Chr(34))
    Execute "Response." & sMethod
  End Function

  Public Function PTMethodEval(sMethodStatement)
    Dim sMethod
    sMethod = Replace(sMethodStatement,"\#", Chr(34))
    PTMethodEval =   Eval( "Request." & sMethod)
  End Function
End Class 'PTWrapper

Dim oPTWrapper
Set oPTWrapper = new PTWrapper
oPTWrapper.PTMethodExecute "Write \#Follow the white rabbit.\#"
oPTWrapper.PTMethodExecute ("Cookies (\#Username\#) = \#Neo\#")

Response.Write "<BR>Username = " & _
            oPTWrapper.PTMethodEval("Cookies(\#Username\#)")

Which produces the following output:

Follow the white rabbit.
Username = Neo

A Caveat on Double Quotes
Remember how we substituted the double quotes (") with \# in order for Eval and Execute to correctly evaluate and execute the statement, respectively? Well, if our pass-through statement needs to contain quotes, we'll need to ensure that the double quotes are doubled. For more on double quotes, read this FAQ. Here's an example:

oPTWrapper.PTMethodExecute "Write \#Follow the \#\#white\#\# rabbit.\#"

would produce:

Follow the "white" rabbit.

Implementation Example
Here's an example of how I've used the pass-through technique. In one of my projects, I had a database application that needed to integrate e-mail notifications to different persons depending on their role within the application. The e-mail notifications were form letters that may need to have their verbiage changed every so often to keep up with application and organizational changes.

The solution involved creating a VBScript object that wraps an e-mail component, such as CDONTS or JMail, in a pass-through object. In this pass-through object, I loaded various form templates depending on the role of the recipient. The pass-through object is manipulated to set up the form letters and send off the e-mails. The pass-through object enabled me to make calls to the 3rd party component as if it were inherent in my own object in addition to other processing that was strongly coupled to the e-mail component (i.e. the e-mail functionality was closely related to other processing I was performing in the VBScript class object).

Conclusion
In this article we examined how to create a "pass-through" object to encapsulate other objects into your own objects or classes using the VBScript Execute and Eval statements. This technique will enable you to easily extend your own objects by encapsulating other objects.

Happy Programming!

  • By Francis Chau


  • Article Information
    Article Title: Object Encapsulation In VBScript
    Article Author: Francis Chau
    Published Date: Wednesday, May 01, 2002
    Article URL: http://www.4GuysFromRolla.com/webtech/050102-1.shtml


    Copyright 2017 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers