Re: Global Variable Question
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.
Re: Global Variable Question
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'.
Re: Global Variable Question
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?