Question : exit from a loop or if statement when a certain condtion is true in c#

I want to exit from a methods loop if a certain codition (i.e. words in the textbox exceed 500) is true. I have used goto and return statement but they dont seem to give me the result i wanted. Any expert ideas
I have attached the code below. When the control goes to end: {return;}  it goes back to  this line   "IterateControls(child); "  in the last if statement ...Is there a way to break from this loop and if statement.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
void IterateControls(Control parent)
    {
        foreach (Control child in parent.Controls)
        {
            int exit = 0;
            if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && child.ID.IndexOf("txt_") == 0)
            {
                TextBox textbox = (TextBox)child;
 
                if (textbox.Text == "")
                {
                    lblError.Visible = true;
                    lblError.Text = "Please Answer All Questions";
                    goto end;
                }
 
                string Answer = Convert.ToString(textbox.Text);
                int count = CountWords(Answer);
 
                if (count > 500)
                {
                    lblError.Visible = true;
                    lblError.Text = "An answer should not carry more then 500 words";
                    exit = 1;
                    goto end;
                    
 
                }
 
 
            }
            
            if (child.Controls.Count > 0 && exit!=1)
            {
                IterateControls(child);
 
            }
        }
 
    end: { return; }
 
 
    }

Answer : exit from a loop or if statement when a certain condtion is true in c#

OK, this should work:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
private bool __ExitIterateControls;
private bool __LoopIterateControls = false;
 
void IterateControls(Control parent)
    {
        bool blnFirstLoop = false;
        if (!__LoopIterateControls) {
            __LoopIterateControls = true;
            __ExitIterateControls = false;
            blnFirstLoop = true;
        }
        foreach (Control child in parent.Controls)
        {
            if (child.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox") && child.ID.IndexOf("txt_") == 0)
            {
                TextBox textbox = (TextBox)child;
 
                if (textbox.Text == "")
                {
                    lblError.Visible = true;
                    lblError.Text = "Please Answer All Questions";
                    goto end;
                }
 
                string Answer = Convert.ToString(textbox.Text);
                int count = CountWords(Answer);
 
                if (count > 500)
                {
                    lblError.Visible = true;
                    lblError.Text = "An answer should not carry more then 500 words";
                    __ExitIterateControls = true;
                }
            }
            
            if (child.Controls.Count > 0 && !__ExitIterateControls)
            {
                IterateControls(child);
            }
            if (__ExitIterateControls)
            {
                break;
            }
        }
        if (blnFirstLoop) {
            __LoopIterateControls = false;
        }
}
Random Solutions  
 
programming4us programming4us