0

I've just started my .Net courses and this is the exercise they made us do at the lab, pretty simple stuff. It worked well at the lab but now that I'm home to retest it, the if condition inside foreach is not working properly. The system is bypassing it automatically for some reason and jumps to first IF outside foreach.

Where am I going wrong with this? Is it that the values are too big "> 1000" ?
Please help me understand what's going on.

private void button1_Click(object sender, EventArgs e)
        {

            // Adds objects in the ArrayList
            f.Clear();
            f.Add(textBox1.Text);
            f.Add(textBox2.Text);
            f.Add(textBox3.Text);
            f.Add(textBox4.Text);
            f.Add(textBox5.Text);

            
  
            int result = 0;

            // foreach loop into the ArrayList
            foreach (object item in f)
            {
                if (int.Parse(item.ToString()) > 1000)
                {
                    MessageBox.Show("Result Too Big. Terminating!");
                    break;
                }
                result += int.Parse(item.ToString());
            }

            if (result > 80)
            {
                MessageBox.Show("Excellent, your score is " + result.ToString());
                
            }
            else if (result > 60)
            { 
                MessageBox.Show("Good, your score is " + result.ToString());
            }

        }
2
Contributors
3
Replies
4
Views
5 Years
Discussion Span
Last Post by cocoll
0

where is your problem?
your code is correct and work perfectly (i try it)

0

For starter, I don't see the MessageBox "Result too big...", when the result of the text boxes is greater than 1000, it simply jumps to the first IF outside the foreach and displays "Excellent, your score is 1500"

0

i don't understand what you want!!!!
with your code if any value in any textbox is greater than 1000,the message "Result Too Big. Terminating" will appear,but if you want if the results of all textboxes is >1000 then you will add another IF in foreach loop for testing the value of "result".

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.