Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

I have repeated code - Instance of a class

Posted on 2009-05-04
4
Medium Priority
?
220 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 1600 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 400 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

Independent Software Vendors: 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

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

916 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