Solved

I have repeated code - Instance of a class

Posted on 2009-05-04
4
217 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET MVC - Views 3 61
ASP.NET Built-In Report Creator / Viewer 5 41
Access-Control-Allow-Origin 4 45
How does this modal work? 3 33
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 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