4
Answers

Breaking out of Recursion

san

san

16y
4k
1
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)