Breaking out of recursion?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Morton

    #1

    Breaking out of recursion?

    I'm recursing through the controls in an asp.net page to find if any TextBox
    contains text.

    JOOI, can I break out of the recursion as soon as it's found one, or does it
    have to unwind itself?

    The TextBoxes are generated at run-time.

    VB.NET 2003, so no Control.FindCon trol.

    Andrew


  • Dick Grier

    #2
    Re: Breaking out of recursion?

    IMO, recursion isn't the way to go. Use a For Each loop, then Exit when
    you've found a (the) textbox you need.

    --
    Richard Grier, MVP
    Hard & Software
    Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
    Edition,
    ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
    2006.
    See www.hardandsoftware.net for details and contact information.


    Comment

    • Bill McCarthy

      #3
      Re: Breaking out of recursion?

      Hey Dick,

      But surely you need recursion to check the child controls of each child
      control and so forth. So I'd imagine it would be along the lines of:

      Function GetFirstTextBox (cntl as control) as textbox
      For each tbx in cntl.Controls.O fType(Of TextBox)
      return tbx
      Next
      For each c in cntl.Controls
      If GetFirstTextBox (c) Isnot Nothing then return Ctype(c,textbox )
      Next
      return nothing
      End Function






      "Dick Grier" <dick_grierNOSP AM@.msn.comwrot e in message
      news:exmywzpYIH A.5900@TK2MSFTN GP02.phx.gbl...
      IMO, recursion isn't the way to go. Use a For Each loop, then Exit when
      you've found a (the) textbox you need.
      >
      --
      Richard Grier, MVP
      Hard & Software
      Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
      Edition,
      ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
      2006.
      See www.hardandsoftware.net for details and contact information.
      >

      Comment

      • Bill McCarthy

        #4
        Re: Breaking out of recursion?

        ooops....


        "Bill McCarthy" <Bill@NOSPAM.co mwrote in message
        news:%23afl04pY IHA.5984@TK2MSF TNGP06.phx.gbl. ..
        >
        Function GetFirstTextBox (cntl as control) as textbox
        For each tbx in cntl.Controls.O fType(Of TextBox)
        return tbx
        Next
        For each c in cntl.Controls
        Dim tbx as textbox = GetFirstTextBox (c)
        If tbx Isnot Nothing then return tbx
        Next
        return nothing
        End Function
        >


        Oh, and to answer the original question, returning out of the recursion as
        above is fine. It won't require an unwinding. Each method finishes/exits
        with the return statement.

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: Breaking out of recursion?

          Andrew,

          AFAIK not really as you need to go back to the main stack in your loop to
          unwind the stacks you have created while doing the recursion.


          Cor


          "Andrew Morton" <akm@in-press.co.uk.inv alidschreef in bericht
          news:uo3RxlpYIH A.3400@TK2MSFTN GP03.phx.gbl...
          I'm recursing through the controls in an asp.net page to find if any
          TextBox contains text.
          >
          JOOI, can I break out of the recursion as soon as it's found one, or does
          it have to unwind itself?
          >
          The TextBoxes are generated at run-time.
          >
          VB.NET 2003, so no Control.FindCon trol.
          >
          Andrew
          >

          Comment

          • Michael C

            #6
            Re: Breaking out of recursion?

            "Andrew Morton" <akm@in-press.co.uk.inv alidwrote in message
            news:uo3RxlpYIH A.3400@TK2MSFTN GP03.phx.gbl...
            I'm recursing through the controls in an asp.net page to find if any
            TextBox contains text.
            >
            JOOI, can I break out of the recursion as soon as it's found one, or does
            it have to unwind itself?
            Yes, it needs to 'unwind' itself. This is no big deal as it's likely to only
            be 2 or 3 levels at most.

            Michael


            Comment

            • Andrew Morton

              #7
              Re: Breaking out of recursion?

              Thanks to all of you for your replies.

              Andrew


              Comment

              Working...