Link to home
Start Free TrialLog in
Avatar of bemara57
bemara57

asked on

Class instance life cycle and accessibility (ASP.NET 2.0 / C#)?

If you instantiate a class on a code-behind, is that instance available for the rest of the user's session, or does it die after the page has been rendered? I know there are many ways to create a class, such as static, so it would really be helpful to me to know the different ways to create a class and how long do they stay alive. Also if I can reference the class from any page or only on the page that the class was instantiated? Thanks in advance for any help in clearing this up.
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

A class created on code-behind is only available to the current compilation of that class, it gets desposed after the page has been rendered. If you want to have a class that persists across pages you can load it into the session collection.
Avatar of Göran Andersson
You can access an instance of a class as long as you have a reference to it.

A class instance can not be static, but the variable that holds the reference can be static.

In web applications, static variables are not very useful. A static variable is shared by all uses, so you can't use it for user specifc data. Also, as several threads can serve pages at the same time, static variables has to be synchronised if you ever want to change the value.

A Page object is created to handle a specific request, so once the response is sent to the browser, the Page object is discarded, along with all the member variables that you declared in the page.

If you want to keep an object longer than the current page, you can put the reference in the Session.Items collection:

Session["identifier"] = someObject;

You should be careful about what you keep in the session, though. If you keep large objects, your application will use up a lot of memory.
Avatar of bemara57
bemara57

ASKER

That's my problem. My object contains huge amounts of data. It is custom settings form the web.config file. These settings are not going to be the same for everybody, BUT they will be the same for large groups of people. So instead of saving the object per user, I would like to save it per group. I can save the group name in the Session["group"] = "GroupA". Then I want to reference the large settings object depending on which group they're in. These settings objects are the ones I want to make globally available, without instantiating. Is this right?

   public static class Globals
   {
      public readonly static SiteSettings GroupA = (SiteSettings)WebConfigurationManager.GetSection("GroupA");

      public readonly static SiteSettings GroupB = (SiteSettings)WebConfigurationManager.GetSection("GroupB");

      public readonly static SiteSettings GroupC = (SiteSettings)WebConfigurationManager.GetSection("GroupC");
   }

Then I can just do GroupA.subclass.method1() from anywhere on my site without initializing at all?
As you want to provide the info for all users you will need to store the object in the Application Collection.
then you would be able to use your class, should be: -

Globals myGlobals = Application["Globals"] as Globals;
myGlobals.GroupA.method1();
I see. But how would I choose which property to use during runtime? What I mean is to literally do something like this:

Session["group"] = "GroupA"
myGlobals.Session["group"].method1();   //instead of myGlobals.GroupA.method1();

The above won't obviously work. But do you see how I'm trying to choose which group property to use dynamically? How can I do this?
I've added an extra function to your Globals Class: -

public static class Globals
{
    public readonly static SiteSettings GroupA = (SiteSettings)WebConfigurationManager.GetSection("GroupA");
    public readonly static SiteSettings GroupB = (SiteSettings)WebConfigurationManager.GetSection("GroupB");
    public readonly static SiteSettings GroupC = (SiteSettings)WebConfigurationManager.GetSection("GroupC");

    public SiteSettings GetConfig(string GroupId)
    {
        switch (GroupId)
        {
            case "GroupA":
                return GroupA;
            case "GroupB":
                return GroupB;
            case "GroupC":
                return GroupC;
        }
        return null;
    }
}
ASKER CERTIFIED SOLUTION
Avatar of McExp
McExp
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
All you need do to complete this solution is to populate the Application collection on app init. This can be done from the Global.asax Application_Init event handler
Excellent! This is exactly what I needed. Thanks a mil for your help!!