san

san

  • NA
  • 19
  • 0

Breaking out of Recursion

Jan 14 2008 5:39 PM
I have a recursive Function, which recurses through all the Controls on the Windows form, checks if there is an Error Message Associated with a textbox, and then returns a bool.

How do I break out of the recursive function the first instance an error message is encountered? What am I doing wrong? Here is the code below -

 public bool RX(Control Ctrlx)
        {
            bool InvalidIPX = false;

            try
            {
                foreach (Control G in Ctrlx.Controls)
                {
                    if (G is TextBox)
                    {
                        if (ErrorProvider.GetError(Ctrl).Length != 0)
                        {
                            InvalidIPX = true;
                            break;

                        }
                    }
                    RX(G);
                }
                return InvalidIPX;
            }

            catch (Exception ex)
            {
                Console.WriteLine("{0} Exception Caught", ex);
                return InvalidIPX;
            }
        }

Answers (4)