Solved

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

Posted on 2007-11-18
9
494 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:bemara57
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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.
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
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.
0
 

Author Comment

by:bemara57
Comment Utility
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?
0
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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();
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:bemara57
Comment Utility
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?
0
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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;
    }
}
0
 
LVL 16

Accepted Solution

by:
McExp earned 500 total points
Comment Utility
The above should now enable you to use something like the following: -

Globals myGlobals = Application["Globals"] as Globals;
myGlobals.GetConfig("GroupA").method1();
0
 
LVL 16

Expert Comment

by:McExp
Comment Utility
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
0
 

Author Comment

by:bemara57
Comment Utility
Excellent! This is exactly what I needed. Thanks a mil for your help!!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now