Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - Storing objects in session variables
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Storing objects in session variables

 Post Reply Post Reply
Author
Rafael_Luna View Drop Down
Newbie
Newbie
Avatar

Joined: 08 April 2003
Location: Mexico
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote Rafael_Luna Quote  Post ReplyReply Direct Link To This Post Topic: Storing objects in session variables
    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.



Edited by Rafael_Luna
Back to Top
pmormr View Drop Down
Senior Member
Senior Member


Joined: 06 January 2003
Location: United States
Status: Offline
Points: 1479
Post Options Post Options   Thanks (0) Thanks(0)   Quote pmormr Quote  Post ReplyReply Direct Link To This Post 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



Back to Top
Rafael_Luna View Drop Down
Newbie
Newbie
Avatar

Joined: 08 April 2003
Location: Mexico
Status: Offline
Points: 2
Post Options Post Options   Thanks (0) Thanks(0)   Quote Rafael_Luna Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.