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());
            }

        }

Recommended Answers

All 3 Replies

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

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"

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".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.