Solved

I have repeated code - Instance of a class

Posted on 2009-05-04
4
214 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

776 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