mmalik15
asked on
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.
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.
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; }
}
Avoid using goto, it has many hidden side effects, istead use if/else, continue and break keywords.
Here, use break keyword:
Here, use break keyword:
void IterateControls(Control parent)
{
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";
break;
}
}
if (child.Controls.Count > 0)
{
IterateControls(child);
}
}
}
And if you want to exit all the functions (recursively), you should use a class field, like this:
private bool __ExitIterateControls;
void IterateControls(Control parent)
{
__ExitIterateControls = false;
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;
break;
}
}
if (child.Controls.Count > 0 && !__ExitIterateControls)
{
IterateControls(child);
}
}
}
ASKER
Thanks for the comments but control still goes to the recursive method IterateControls(child);
I had already told earlier that you need to change the logic as it is a recursive call and call stack will show you the exact code control.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
IterateControls(..)functio