1

I want to count and sum all values in a ListBox. For example, I have the following values in my ListBox: 4, 6, 1, 7. My current value is 18. If I add a 2 with a TextBox, I need to get 20 and if I add 5 I need to get 25 in total.

If I try it with the below code, it gives me a whole other number.

Here is my code:

private void AddButton_Click(object sender, EventArgs e)
{
    decimal sum;
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = "";

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        sum += Convert.ToDecimal(listBox2.Items[i].ToString());
    }

    Label1.Text = sum.ToString();
}
1
  • 3
    What other number does it give you? Can you step with the debugger through the loop and check each value that's getting added up? Small tip: Use a foreach instead of a for loop to iterate over the items Commented Mar 13, 2019 at 13:19

5 Answers 5

2

You missed to initialize default value of sum. Assign sum = 0 before adding values to sum variable

private void AddButton_Click(object sender, EventArgs e)
{
    decimal sum = 0;  //Set sum = 0  by default
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = "";

    for (int i = 0; i < listBox2.Items.Count; i++)
    {
        //To check value of sum after each iteration, you can print it on console
        Console.WriteLine("Sum =" +sum);
        sum += Convert.ToDecimal(listBox2.Items[i].ToString());
    }

    Label1.Text = sum.ToString();
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nope i tried it if i had the following numbers 4,5 and 6 it shows as result 28
Debug your code and check value of sum. Console.WriteLine will definitely helpful here.
1

Sorry for maybe not answer but it is strange that your compiler didn't tell you to initialize sum. The second I tested your code and it works correctly as expected that means that if the problem is not in sum variable then you made something else to this fields somewhere else so your code doesn't work correctly.

Having in mind your comment to previous person I'd say the same. In some cases (I know it's funny but) you may have viruses on your computer. Check it. Once in my life I've failed my maths lab work because of virus that interrupted my program so it drew the wrong chart !sick I know :D

2 Comments

agreed with your first para but not with second para. Only because of default initialization code was giving weird results. still +1 for first para...
@PrasadTelkikar man said he tried initialized and still had the problem xD
0
    private void button1_Click(object sender, EventArgs e)
    {
        var sum = 0;
        var value = 0;

        listBox1.Items.Add(textBox1.Text);

        foreach (var item in listBox1.Items)
        {
            if (!int.TryParse(item.ToString(), out value))
                continue;

            sum = sum + value;
        }

        label1.Text = sum.ToString();
        textBox1.Text = string.Empty;
    }

Comments

0

Use of unassigned local variable 'sum', sum must be assigned before use !

decimal sum = 0;

Rest all is ok

Comments

0

Need to set sum to 0 and also it is probably best to use a foreach instead of a Dotloop.

private void AddButton_Click(object sender, EventArgs e) {
    decimal sum = 0;
    listBox2.Items.Add(TextBox1.Text);
    TextBox1.Text = ""; 
    foreach (string s in listBox2) { 
        sum += Convert.ToDecimal(s);
    } 
    Label1.Text = sum.ToString(); 
}

1 Comment

foreach (var s in listBox2.Items) { ... } please correct it.You need to iterate though items not with ListBox. Your code is buggy please correct it

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.