0

I wrote a few code line in c# to iterate through a list, but i print in the textbox only the last one. the code that i wrote:

//For instantiation
Account account = new Account(0,"","", 0); 

//A list for class Account
List<Account> listAccount = new List<Account>();

//Button for adding new Customer
    private void button1_Click(object sender, EventArgs e)
    {
        account.CustomerID = int.Parse(customerIdTxt.Text);
        account.CustomerFullName = customerNameTxt.Text;
        account.CustomerAddress = customerAddrTxt.Text;
        listAccount.Add(account);

    }

    //For printing the Customer's detailes in textbox
    private void button6_Click(object sender, EventArgs e)
    {
        string showCustDetailes = "";
        for(int i=0;i<listAccount.Count;i++)
        {
            showCustDetailes+=
                "Customer ID        : " + listAccount[i].CustomerID + Environment.NewLine +
                "Customer Name      : " + listAccount[i].CustomerFullName + Environment.NewLine +
                "Customer Address   : " + listAccount[i].CustomerAddress + Environment.NewLine +
            "---------------------------------------------------" + Environment.NewLine;
        }
        viewDetailesTxt.Text = showCustDetailes;
    }

can anyone help me how can i print the whole customers list

4
  • When you say "The last one" do you mean only the "Customer Address" line, or do you mean only one complete ID, Name and Address line? Commented Jun 24, 2015 at 15:57
  • Also make sure that your TextBox is a multi-line textbox otherwise it will only support a single line. Commented Jun 24, 2015 at 15:57
  • 1
    You're only using one Account instance. Commented Jun 24, 2015 at 15:59
  • Ouch, use StringBuilder not string += Commented Jun 24, 2015 at 16:01

2 Answers 2

7

There's nothing wrong with the code that loops over the list (apart from not using foreach()). If you really only see one account, the problem is in displaying: make your textbox bigger or give it scrollbars.

Also, you're editing the same instance of account every time, so you're filling your list with multiple references to the same account. You must use new Account to instantiate a new one for every "button 1" click:

private void button1_Click(object sender, EventArgs e)
{
    Account account = new Account(0,"","", 0); 
    // ...
    listAccount.Add(account);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Actualy the code that should be written is that: "Accoount account" on the top of class, and then in one of the relevent function to instantiate by the code:" account=new Account(0,"","",0) ".
No, you don't need that. If you don't use the account elsewhere, it's better to give it a scope as small as possible: in the button1_Click method.
0
List<Account> listAccount = new List<Account>();

private void button1_Click(object sender, EventArgs e)
{
    var account = new Account {
        CustomerID = int.Parse(customerIdTxt.Text),
        CustomerFullName = customerNameTxt.Text,
        CustomerAddress = customerAddrTxt.Text
    };
    listAccount.Add(account);
}

private void button6_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    foreach(var account in listAccount)
    {
        sb.AppendFormat("Customer ID: {0}\nCustomer Name: {1}\nCustomer Address: {2}\n\n", account.CustomerID, account.CustomerFullName, account.CustomerAddress);
    }
    viewDetailesTxt.Text = sb.ToString();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.