Solved

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

Posted on 2007-11-18
9
499 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 16

Expert Comment

by:McExp
ID: 20307653
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
ID: 20307732
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
ID: 20307836
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:McExp
ID: 20307874
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
 

Author Comment

by:bemara57
ID: 20308142
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
ID: 20308184
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
ID: 20308187
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
ID: 20308199
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
ID: 20308225
Excellent! This is exactly what I needed. Thanks a mil for your help!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

728 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