Breaking out of recursion?

  • Thread starter Thread starter Andrew Morton
  • Start date Start date
A

Andrew Morton

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.FindControl.

Andrew
 
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.
 
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.OfType(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
 
ooops....


Bill McCarthy said:
Function GetFirstTextBox(cntl as control) as textbox
For each tbx in cntl.Controls.OfType(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.
 
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 said:
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
 
Back
Top