Solved

I have repeated code - Instance of a class

Posted on 2009-05-04
4
216 Views
Last Modified: 2012-05-06
I have a class and I'm instantiating it in 3 places in my aspx page.

BasketPayment basketPayment = new BasketPayment();

1. ***first issue is the repeated code
2.*** Second issue: I have a dropdownlist event. I create the instance in that event as well. Everytime user changes the ddl value, that class is instantiated.This poses a problem because  I have a List property in the class. With each class instantiation, that list is cleared.

How can i not repeat this line of code and use it in the ddl event WITHOUT clearing the list.
I was thinking about maybe creating a property in my aspx page. Check the instance of this class..if NULL, create the instance, otherwise, just use the instance that was created. Can i do this...

Public class BasketPayment
 {
   ....
   public List<CreditCard> creditcardList
       {
           get
           {
               if (_creditCardList == null)
               {
                   return _creditCardList = new List<CreditCard>();
               }
               return _creditCardList;
           }
           set
           {
 
 
 
               _creditCardList = value;
               
 
           }
       }
 
 
}

Open in new window

0
Comment
Question by:Camillia
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 400 total points
ID: 24298625
Hi,

Why not use a session variable or similar? i.e.:

if (!Page.IsPostBack) {
    BasketPayment basketPayment = new BasketPayment();
    session["basketPayment"] = basketPayment;
}

if (Page.IsPostBack) {
    if (!session["basketPayment"] == null) {
        BasketPayment basketPayment = session["basketPayment"]
    }
}

Sorry for any coding mistakes, I'm not a C# developer, I work in VB.

/Carl.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24298742
let me try. Thanks. Will post back
0
 
LVL 8

Assisted Solution

by:ppittle
ppittle earned 100 total points
ID: 24298932
Frazadw,

You intial code is correct, that's called the Singleton pattern.  carlnorrbom was also correct, in that if you need to persist the List across postbacks you'll need to store it either in Session or ViewState.  (Personally, I prefer ViewState, as long as the object is small, because ViewState works better in a web farm scenario).  Anyway,  this is a little cleaner:

public List<CreditCard> creditcardList
{
get
{
    object o = ViewState["creditcardList"];
    if (null == o){ ViewState["creditcardList"] = new List<CreditCard>(); }
    return (List<CreditCard>)ViewState["creditcardList"];
}
set
{
ViewState["creditcardList"] = value;
}
}

Feel free to replace ViewState with Session if you prefer.
0
 
LVL 7

Author Comment

by:Camillia
ID: 24298940
pplittle, let me try your code as well.
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
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 …
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…

679 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