I created a class and used it successfully - as a Session variable - in one
ASP page. In the next page, the session variable still existed, was still
of the correct type (I checked with the TypeName() function), but none of
its properties or methods worked ("property not supported", apparently...).
Can anyone confirm this or suggest otherwise?
-Chris B.
What do you mean by the following? Could you show us the code?
> I created a class and used it successfully - as a Session variable - in one
> ASP page.
Offhand my guess is that:
a) you'll need to include the class definition in the second ASP page
and
b) also that you may need to copy the object from the Session object to
a local object.
I have not tried this, but will later once I get a chance, since you've
triggered my curiosity.
BTW the article at
http://msdn.microsoft.com/library/periodic/period99/classes.htm
suggests not storing VBScript Class objects in the Session or
Application object because of threading problems.
"Christopher Brown" <cbr...@webnet.fr> wrote in message news:e8HSTLVf$GA.212@cppssbbsa05...
Well, so the official line is you shouldn't store these objects in
Session or Application variables. But you can store their *state* into
those variables without incurring any threading problems. Here's an
example.
VBScriptObjectStore.asp - Run this first.
Creates an object, sets object instance variables, and finally destroys
the object. But as part of the object's termination event, stores the
object's (state) variables to the Session Object with specified names.
VBScriptObjectRetrieve.asp - Run this second.
Creates an (empty) object, then sets it's name. Setting it's name runs
an object method (GetInstance) that retrieves values from the Session
Object associated with that instance of that object class.
The object instance's values could be stored to any persistent storage.
Possibilities are
a) Session Object,
b) Application Object,
c) text file,
d) binary file (you'll have to write a component until VBScript gets an
intrinsic binary write file),
e) other database,
f) MoonBounce(you bounce the data off the moon, thus effectively storing
it in the ether).
=========================================================
File VBScriptObjectDefinition.inc:
=========================================================
<% OPTION EXPLICIT
Class myClass
' Instance name and public variables.
Public strName
Public strSillyString
public intSillyInteger
public curSillyCurrency
'
' The following two event methods are optionally available
' for all objects. The first is called when the object is created,
' the second when the object is destroyed.
'
PRIVATE SUB Class_Initialize
strName = ""
strSillyString = ""
intSillyInteger = 0
curSillyCurrency = 0.00
END SUB
PRIVATE SUB Class_Terminate
' Save current state to the Session Object.
Freeze( strName )
END SUB
PUBLIC SUB GetInstance( strInstanceName )
'
' Set the object instance's name. Also if any values
' associated with this class and instance name are in the
' Session Object, retrieve them.
'
strName = strInstanceName
Thaw( strName )
END SUB
'
' Subroutines to freeze (save object variables to Session Object) and
' thaw (retrieve object variables from Session Object).
'
PUBLIC SUB Freeze( strName )
Session( "myClass::" & strName & "::SillyString" ) = strSillyString
Session( "myClass::" & strName & "::SillyInteger" ) = intSillyInteger
Session( "myClass::" & strName & "::SillyCurrency" ) = curSillyCurrency
END SUB
PUBLIC SUB Thaw( strName )
strSillyString = Session( "myClass::" & strName & "::SillyString"
)
intSillyInteger = Session( "myClass::" & strName & "::SillyInteger"
)
curSillyCurrency = Session( "myClass::" & strName & "::SillyCurrency"
)
END SUB
PUBLIC SUB Print
Response.Write "<br>An object of class myclass:"
Response.Write "<br>strName(Instance Name) = " & strName
Response.Write "<br>strSillyString = " & strSillyString
Response.Write "<br>intSillyInteger = " & intSillyInteger
Response.Write "<br>curSillyCurrency = " & curSillyCurrency
Response.Write "<br>End of object............<br>"
END SUB
END CLASS
%>
=========================================================
File VBScriptObjectStore.asp:
=========================================================
<!-- #INCLUDE FILE = VBScriptObjectDefinition.inc -->
<%
Dim objOfMyDesire
'
' Create an object of class myClass.
' Print the state of the object.
' The variables should be initialized.
'
Set objOfMyDesire = NEW myClass
objOfMyDesire.Print
'
' Change the object instance variables and print.
'
objOfMyDesire.strName = "Mary"
objOfMyDesire.strSillyString = "I Object, Your Honor!"
objOfMyDesire.intSillyInteger = 1234567890
objOfMyDesire.curSillyCurrency = 56789.22
objOfMyDesire.Print
'
' Destroy the object, which saves state to Session Object.
'
Set objOfMyDesire = Nothing
'
' Print Session Object variables to ensure
' the object instance's values are stored.
'
Response.Write "<br>Session Object variables:"
Dim key
For Each key in Session.Contents
Response.write "<br>" & key & " = " & Session.Contents( key)
Next
Response.Write "<br>End of Session Object variables...<br>"
%>
=========================================================
File VBScriptObjectRetrieve.asp:
=========================================================
<!-- #INCLUDE FILE = VBScriptObjectDefinition.inc -->
<%
Dim objOfMyDisgust
'
' Create a new object instance of class myClass.
' The variables should be initialized to
' default values. Print the state of the object.
'
Set objOfMyDisgust = NEW myClass
objOfMyDisgust.Print
'
' Set the object instance's name: this will
' load any saved data from the Session Object.
'
objOfMyDisgust.GetInstance( "MARY" )
objOfMyDisgust.Print
'
' Destroy the object.
'
Set objOfMyDisgust = Nothing
%>
"Michael D. Kersey" <mdke...@hal-pc.org> a écrit dans le message news:
38B30730...@hal-pc.org...
> Christopher Brown wrote:
> > Can VBScript classes be stored as Session variables?
> > I created a class and used it successfully - as a Session variable - in
one
> > ASP page. In the next page, the session variable still existed, was
still
> > of the correct type (I checked with the TypeName() function), but none
of
> > its properties or methods worked ("property not supported",
apparently...).
> > Can anyone confirm this or suggest otherwise?
> > -Chris B.
>
> What do you mean by the following? Could you show us the code?
> > I created a class and used it successfully - as a Session variable - in
one
> > ASP page.
>
In InterDev use a page control and call the class that way in other pages.
OR
Use an include file called "BLAH.vbs" and include it in all the pages. You
classes would be avaiable.
Problems - if you are trying to maintain state or info there MIGHT be a problem
with the above functionality. You could always hide the info in hidden fields
and carry across to other forms......
Just a suggestion.
Vince