Print Page | Close Window

Storing objects in session variables

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=1703
Printed Date: 29 March 2026 at 5:16am
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: Storing objects in session variables
Posted By: Rafael_Luna
Subject: Storing objects in session variables
Date Posted: 08 April 2003 at 8:14pm

Hi, I have this problem:


I use some classes in my pages that I create using:


<% class MyClass


  public function MyMethod()


  end function


end class %>


I create an instance and store it on a session variable:


set session("MyClass") = New MyClass


so I can get the object in every page of my app, using:


set MyObj = Session("MyClass")


In the page I create the instance, I can acces the class methods with no trouble, The problem is that when I'm in another page ASP recognize the session variable as an object, but can't access the class methods.


What am I doing wrong?


Sorry about my engilsh.


Thanks.




Replies:
Posted By: pmormr
Date Posted: 09 April 2003 at 4:48pm

If you wish to have this "session" variable avaliable to every page on your site then you should place it into the global.asa file in the root directory of your site. Place this code in the file...

<SCRIPT LANGUAGE="VBScript" RUNAT="Server">

'Application_OnStart runs when you start IIS
Sub Application_OnStart

class MyClass
  
  public function myMethod()
 
  'your code goes here...

  end function
end class

'Create and instane of "my class" in application scope
Application("MC") = New myclass

End Sub

</SCRIPT>


Phew! Now to call the instance of myclass from anywhere in your site just call Application("MC"). You can use this in the same way, same code and all with a Sub Session_OnStart which will create the object when the user's session starts.

I wouldn't use a class for this unless it was absolutely necessary, classes get sort of corky in asp...

Hope I helped,

Paul Morgan
Webmaster





-------------
Paul A Morgan

http://www.pmorganphoto.com/" rel="nofollow - http://www.pmorganphoto.com/


Posted By: Rafael_Luna
Date Posted: 09 April 2003 at 7:36pm
I don't want an Application variable, I want every user to have their own objects, so these must be session variables.

And I don't want to create these instances on every session start neither, just when is really needed.

Thanks anyway.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net