Click to See Complete Forum and Search --> : Global Variable Question
kleinma
Jan 14th, 2005, 12:13 PM
I searched on here and found a few topics, but not sure that any of them really answer my question.
My ASP.NET application references a class library I wrote. It has classes like customer, customerproducts, products, etc...
so I want to have a global variable that is set when a customer would login to the site, for the customer class, and this variable would be carried across the entire asp.net application, so basically
Public CurrentCust as New Customer
and then in any given aspx page I would access this variable
so I am looking to know if this is possible using a variable declared public in the same fashion as lets say a module in a VB.NET application, or if I need to use session variables instead.
What is the best course of action for this type of situation?
jpfolin
Jan 15th, 2005, 09:53 PM
You do not want to use a global variable because it will be the same value for everyone that uses the application. You want to use a session variable instead.
mendhak
Jan 15th, 2005, 09:57 PM
Both ways would work. I'd use a session variable in such a case because it'll timeout after a while and save me the trouble of having to 'detect activity'.
jbalz
Jan 25th, 2005, 12:16 AM
I have a very similar problem - except much more complicated. I have 3 classes that store users across the scope of the application. However, the only way I've found to be able to use them is by making an Application variable and adding it into a page. As an example, every user who gets on my site has a "WebUser" class created for them. Each WebUser instance variable is stored in a WebUserDictionary, which is derived from (Inherits) System.Collections.DictionaryBase. The WebUserDictionary is a member of class Observer, and I need Observer to be accessible from every page in the application. I've tried various approaches, but each one fails my needs for some reason:
--Shared class: I created a class that keeps Observer as a static member. Therefore, whenever I wanted to access the Observer instance, I could type ObserverHolder.Observer from any page or class in the application. The problem was that the scope of the Observer class would end once I moved to a different page. So, if I opened a page1.aspx and a WebUser was created and stored in Observer's WebUserDictionary, then when I opened up page2.aspx, the WebUserDictionary was empty.
--Application variable: I couldn't figure out how to make a static variable from the application. So, I stored the Observer, with its WebUsers dictionary, in an application variable, accessible from any page as Application("Observer").Observer.WebUsers. However, every time I accessed the variable, it had to be manually updated. Therefore, I have to:
1) Retrieve the Observer from the application variable.
2) Pass the variable on to any classes that need to use the Observer.
3) Update the Application variable with the new Observer class, if any changes were made to it.
This is way too many steps to have to maintain a current, usable list of web users on my site. It's not thread-safe at all, and locking the application variable would slow the entire process down to make it useless. The type of functions that the WebUser class performs make it impossible to store the data in Session variables. Plus, there are two more Observer-like classes that track AdministratorUsers and OperatorUsers.
Is there any way to keep a global class variable in memory that is accessible across all pages?
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.