Can Not Store ASP Classic Object in Session
In ASP Classic you can define an object using syntax like “public class X” and define properties and getters and setters. This is fairly common practice and attempts to bring the scripting language to approach an object oriented programming point of view.
I recently ran across the need to stuff these objects into the session for later retrieval. The interesting thing is the code to add the object to the session and retrieve from the session appears to work without any error. The only error occurs when you attempt to access any method or property within the realized object.
After some digging it looks like this is a planned feature from Microsoft. I guess the thinking is that the ASP engine can not guarantee there is a type definition around (the ASP page that defines the class), therefore it doesn’t really do anything with your session operations.
My alternative was to serialize the object and store the serialized version in the session, something like:
class X
public var1
public var2
public Function toString
toString = var1 & "~" & var2
end Function
So, we can have code to store the ‘object’ in the session like:
Session("myobject") = oX.toString()
Later we can retrieve and realize the object using the reverse code:
Set oX = new X()
X.fromString(Session("myobject"))
Where fromString is defined as:
class X
public Sub fromString(s)
Dim array
array = Split(s,"~")
var1 = array(0)
var2 = array(1)
end Sub
It’s a pain to add the serialization code to your ASP classes, but it works fine.
