Solved

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

Posted on 2007-11-18
9
496 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
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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

790 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