Solved

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

Posted on 2007-11-18
9
495 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
A short film showing how OnPage and Connectwise integration works.

930 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