본문 바로가기

말랑말랑한 이야기

Saving ASP VBScript "Objects" to Session

Saving ASP VBScript "Objects" to Session (Tuesday, March 08, 2005)
Continuing my trouble and hatred of anything todo with Visual Basic, especially VBScript.  In this episode of The VB Sucks Edition, I'll reivew the problem with VBScript "Objects”.  As any programmer knows, VBScript was never meant to be object-oriented, but by VBS 5, someone at Microsoft thought they could try to make it a bit OO compatible.  The introduction of VBS classes was a major step in that direction.  This seems like a good idea, but instead of making real objects, Microsoft hacked together classes that really mean nothing.  Let me explain.

VBS objects are not really stored in memory as objects.  For this reason, it is not possible to store a VBS object in the Session or Application variables in ASP.  This creates huge headaches, and is completely rediculous.  There are however, some solutions.

The first solution is "Serializing" your class.  You will not truely be serializing anything, but rather creating a "seralize" and "deserialize" method for each class that you declare.  These methods will take a Session variable name as a parameter and "store" or "retrieve" a class from session.  See the example below.

Class MyClass
     Dim Var1
     Dim Var2
End Class

Function Serialize_MyClass( instance, sessionVar )
     Session( sessionVar & ".Var1" ) = instance.Var1
     Session( sessionVar & ".Var2" ) = instance.Var2
End Function

Function Deserialize_MyClass(sessionVar)
     Dim instance : Set instance = new MyClass
     instance.Var1 = Session( sessionVar & ".var1" )
     instance.Var2 = Session( sessionVar & ".var2" )

     Set Deserialize_MyClass = instance
End Function

This solution will work if you must keep all your code in VBScript, however is quite a pain to implement.  If you can mix JScript into your code, I suggest using the following solution.

The other way of saving objects to session in ASP is not to use VBScript objects, but use JScript objects instead.  Because of the way JScript creates objects, you can save JS objects to Session.  By declaring your class in JS, you can keep your code (somewhat) clean, and still have the ability to save your objects.  See below for an example.

<script runat="server" language="jscript">

function MyClass()
{
   this.Var1 = Var1;
   this.Var2 = Var2;
}

var Var1, Var2;

function MyClassFactory()
{
   return new MyClass();
}

</script>

Using script like the one above allows you to keep your class declarations clean, and removes the need for "serialization” functions.  The only problem that still remains is VBScript can't declare instances of JScript classes (even though it can use them).  For this reason, I suggest using a factory function in JScript for your classes (such as MyClassFactory in the example above).  Because VBScript can call JScript functions, you will be able to get a new instance of the JScript class into your VB code, as shown below.

Dim instance : instance = MyClassFactory()
instance.Var1 = "12345"


So there you have it, the half-ass hack to solve the problem of saving classes to Session or Application variables to the piece of junk that is VBScript.

Filed under: