Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2009-02-09
6
Medium Priority
?
1,501 Views
Last Modified: 2013-11-25
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.

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

Open in new window

0
Comment
Question by:mmalik15
  • 3
  • 2
6 Comments
 
LVL 6

Expert Comment

by:Mushq
ID: 23588582
Hi,
IterateControls(..)function is recursive function so it is behaving like that, you can check the call stack in visual studio editor, so you may need to change your logic so that function shouldn't become a recursive function.
0
 
LVL 4

Expert Comment

by:zer0se7en
ID: 23588627
Avoid using goto, it has many hidden side effects, istead use if/else, continue and break keywords.

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

Open in new window

0
 
LVL 4

Expert Comment

by:zer0se7en
ID: 23588640
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);
            }
        }
    }

Open in new window

0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:mmalik15
ID: 23588694
Thanks for the comments but control still goes to the recursive method IterateControls(child);
0
 
LVL 6

Expert Comment

by:Mushq
ID: 23588703
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.
0
 
LVL 4

Accepted Solution

by:
zer0se7en earned 2000 total points
ID: 23588734
OK, this should work:

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

Open in new window

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…

575 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question