Results 1 to 4 of 4

Thread: Global Variable Question

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Global Variable Question

    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?

  2. #2
    Lively Member
    Join Date
    Mar 2002
    Posts
    96

    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.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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'.

  4. #4
    New Member
    Join Date
    Jan 2005
    Posts
    1

    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width