Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 10th, 2007, 02:12 PM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default Global declaration of a class object

I want to create an instance of a Class. The problem is that i want to create this instance once when the session starts and i should have access to this instance in my all other .aspx pages. Can anyone suggest where i should create this instance so that it will be available on all .aspx pages?

Thanks.

 
Old October 10th, 2007, 02:15 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. can you explain a little further why you need this?? there could be a case that your memory leak bad b/c you never end that object...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old October 10th, 2007, 09:19 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Or, there may be a better design solution for what you are trying to achieve.

-Peter
 
Old October 11th, 2007, 11:42 PM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am declaring a class called SmartSessionPage.cs in the App_Code folder as-
public class SmartSessionPage : System.Web.UI.Page
{
    public SmartSessionPage()
    {
    }
    public virtual StoreCart ShoppingCart
    {
        get
        {
            return ((StoreCart)Session["ShoppingCart"]);
        }
        set
        {
            Session["ShoppingCart"] = value;
        }
    }
}

StoreCart is again a class defined in the same folder in which i am declaring functions to add items to the cart, to remove item from cart...etc. I want to make an instance of this SmartSessionPage class so that it gets initialized only once whenenver a new session starts.
Please suggest me the best way to achieve this.

Thanks

 
Old October 12th, 2007, 08:56 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

First, you are creating a "utility" class that is derived from the page. This class will never be instantiated so it just won't work the way you are expecting it to.

The way I would normally attack a problem like this is:
- Create a class that serves as the base class for all the pages on which you want to use the shopping cart
- Put your shopping cart property in this class
- All pages that derive from the class will have access to the cart
- Make the property read-only and use an instantiate-on-demand pattern

Here's what your base class code should look like:
Code:
public abstract class SmartSessionPage : System.Web.UI.Page
{
    private StoreCart _storeCart; //This is the local page instance

    protected StoreCart ShoppingCart
    {
        get
        {
            if(_storeCart == null) //Is the local instance empty?
            {
                if(Session["ShoppingCart"] != null) //Is the cart in session?
                {
                    _storeCart = (StoreCart)Session["ShoppingCart"]; //Get it from session
                }
                else //No cart in session
                {
                    _storeCart = new StoreCart(); //Create new cart instance
                    Session["ShoppingCart"] = _storeCart; //Put it in session
                }
            }
            return _storeCart;
        }
    }
}
Now, any page that you want to have access to the cart will need to inherit from your new class. For example:
Code:
public class SomePage : SmartSessionPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.ShoppingCart.AddItem();
    }
}
The GETTER for the ShoppingCart property does two important things:
- It checks to see that the local page instance hasn't already been set. If it is, it just returns it. This will always happen once per page life cycle.
- When the local instance hasn't been set, it checks to see if it's in session. If it is, it restores it from session into the local page instance so it's usable throughout the rest of the page cycle. If it isn't in session, it creates the new instance of the StoreCart and puts that into session.

The getter performs all the logic to create the shopping cart when it's needed so your individual pages don't have to worry about it.

-Peter
 
Old October 14th, 2007, 01:21 PM
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter,
             It really works and my problem is solved...

Thanks once again.





Similar Threads
Thread Thread Starter Forum Replies Last Post
using Class in Global.asax oveisi Classic ASP Components 1 July 21st, 2008 03:42 PM
Using Global.asax varialbles in a class.vb file sougata ASP.NET 1.0 and 1.1 Basics 3 June 30th, 2005 07:33 AM
Global Connection Object sarahmapg ADO.NET 1 May 18th, 2005 06:00 AM
Incomplete Class Declaration gillianbc C++ Programming 2 November 19th, 2004 06:10 PM
Global ADO Connection object DaveParry123 ADO.NET 0 October 7th, 2003 03:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.