Solved

I have repeated code - Instance of a class

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

8 Experts available now in Live!

Get 1:1 Help Now