Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Storing object into a session variable.

Status
Not open for further replies.

nimapoggy

Programmer
Mar 4, 2002
34
0
0
FR
Hi !
I'm trying to set a session variable as an object :
***code begins here********
Class Engin
Private modVersion
Private modName
private modCat

Private Sub Class_Initialize
modVersion = "1.0"
modName = "Kernel Engin"
modCat = "base module"
End Sub

Public Property Get Version
Version = modVersion
End Property

Public Property Get Label
Label = modName
End Property

Public Property Get Category
Category = modCat
End Property

Public Property Get getInfos
getInfos = &quot;Version: &quot;&Version&&quot;<br>Label: &quot;&Label&&quot;<br>Category: &quot;&Category&&quot;<br>&quot;
End Property
End Class

If IsObject(Session(&quot;oEngin&quot;)) Then
response.write(&quot;session is already active<br>&quot;)
Else
Set Session(&quot;oEngin&quot;) = New Engin
response.write(&quot;session activated<br>&quot;)
End If

response.write(Session(&quot;oEngin&quot;).getInfos)
***code ends here********
This returns the following error (in french):

Type d'erreur :
Erreur d'exécution Microsoft VBScript (0x800A01B6)
Cet objet ne gère pas cette propriété ou cette méthode: 'Session(...).getInfos'
/test/kernel.asp, line 37

that means (translated in english):
Error Type :
Error whille executing MS VBscript (0x800A01B6)
this object does not support this property or method: 'Session(...).getInfos'
/test/kernel.asp, line 37

Help me please !!!
Thanks, nicolas.
 
Did you ever get an answer to this? I'm trying to do something similar.
 
IMHO, the definition of getInfos had errors:
Public Property Get getInfos
getInfos=&quot;Version: &quot; & modVersion & &quot;<br>Label: &quot; & modName & &quot;<br>Category: &quot; & modCat & &quot;<br>&quot;
End Property

Hope This Help
PH.
 
PHV > this not the point... the getInfos prop is absolutely correct (version, label and category are public props, so are also accessible from within the class...)

JPA > sorry no answer at all... people who writes code using vbscript don't use objects... and don't know how to use objects... I only recieved inappropriates answer such as PHV one.
 
I'm not sure why this question wasn't asked in the ASP forum, since it isn't a VBScript question. The same issue exists if using VBScript, JScript, or some 3rd party script engine in your ASP pages.

But let's look at what we have here anyway...

Expressions written like:
Code:
Version: &quot;&Version&&quot;<br>Label: &quot;&Label&&quot;<br>Category: &quot;&Category&&quot;<br>&quot;
... tend to be prone to obscure errors, mostly because when something like &H or &O is seen by the parser they look like hex or octal literals.
Code:
Version: &quot; & Version & &quot;<br>Label: &quot; & Label & &quot;<br>Category: &quot; & Category & &quot;<br>&quot;
... is the proper way to write string concatenation.

But your real problem is that:
Code:
Set Session(&quot;oEngin&quot;) = New Engin
doesn't do what you might think it does.

It will create an instance of class Engin all right. It will also create a new element in the Contents collection of the Session object called &quot;oEngin&quot; and set it to a reference to the just-created instance of the Engin class.

That is, a reference to an object, not an object. The object itself does not get cached in Session.

But this object is a Page scope object. Once the page script exits, the object is forcibly deallocated by ASP's &quot;scope exit&quot; cleanup. Now you'll have something called &quot;oEngin&quot; in the Session.Contents collection all right. It is even an object reference (which is what
Code:
IsObject()
tests for). Problem is, it's a reference to an object that doesn't exist anymore! When you come back into this page again from the same browser window (and ergo, the same session) your script falls over.

I would have thought that a Windows Script Component might be an answer to this quandry instead. Problem is, these are limited to Page scope as well - due to threading limitations for one thing.

So about the only place that VBScript classes are even useful is in DHTML and in desktop scripts as far as I can tell. They work fine there of course. And they work just fine in ASP pages as well - as long as you use them within the page and not between pages (which means within one request).


The other blind alley would be a VB6 class, compiled as an ActiveX DLL. These actually work wonderfully for many things. But you really don't want to persist them across pages either (even though you can). You'll end up locking either each session or each application down to one thread - with nasty consequences for a web server that actually does much.


Also AFIAK, the proper way to accomplish what is shown in the initial post's sample code is to just use normal &quot;user&quot; Session variables stored in the Contents collection instead of trying to construct some aggregate as a VBScript class:

Session(&quot;Version&quot;)
Session(&quot;Label&quot;)
Session(&quot;Category&quot;)

Then go ahead and declare a
Code:
Function getInfos()
if you must to perform this seemingly-occasional bit of string concatenation.
 
A better way would be a function to serialize the object and store the resulting string in a session, then unserialize it at page loading. the danger with properties stored directly in sessions variables is that they can be modified outside the object or without the use of a specific method.
I know that serializing is not a much secure way to prevent that because it generates a string so it can be modified as well. But it's no so easy and as developpers are lazy (as I am) they would prefer to unserialize and use the object. I hope...
 
Wel since a Session is unique within an Application, I'm not sure where any fear of other developers' actions might enter into it unless the team isn't mutually trustworthy.

There are lots of solutions though actually. For example you might want to market (even if only internally) a set of such classes for ASP use.

This just requires other tools besides VBScript or VB. It is more of a threading issue than anything to do with the support for objects in script or in VB though.

This is why it is possible to cache object references in Session generally - those objects were built for it.

It's also the reason why a VB6 object can be cached too, but why it is a Bad Thing. The apartment threading of VB6 ends up locking your Session or Application down to one IIS thread and causes nasty performance problems.

Much of this was alleviated in ASP+ a.k.a. ASP.Net but we don't all have the luxury of that option. A good piece of what was done there was actually implemented according to the pattern provided by VB6 Web Classes. There might be an answer for you there too yet, if you are willing to use deprecated technology. Then again ASP is considered obsolete as well too, so you'd have nothing to lose.

;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top