473,320 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Excaping recursive functions

Hi Everyone,
I got the following code from Deborah Kurata's book. I need to return the
control that matches the name that I pass into the function. It does iterate
through the form but it will not return a control reference. Following the
code, it does actually get a reference, but when it returns from one of the
recursize calls, the code keeps going. For examle:
First call to FinControl
Has child so call FindControl again
Finds control, returns the reference
'Comment I need a garnteed way of exiting now
Keeps looping, and if a control has child again, call FindControl again

Private Function FindControl( ByVal ControlName As String, ByVal CurrentForm
As control) As Control
Dim rtn As Control
For Each ctrl As Control In CurrentForm.Controls
If ctrl.Name = Controlname then
rtn = ctrl
exit For
End If
If ctrl.HasChildren then
FindControl(ControlName, ctrl)
End If
Next ctrl
return rtn
End Function

What is the best way to handle this one. Thanks for any info.
Michael

Nov 21 '05 #1
7 1218
Michael,
I would expect:

| If ctrl.HasChildren then
rtn = FindControl(ControlName, ctrl)
Exit For
| End If

However I would not define the rtn variable, I would simply use the Return
statement when I found an item. Something like:

| Private Function FindControl( ByVal ControlName As String, ByVal
CurrentForm
| As control) As Control
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname then
Return ctrl
| End If
| If ctrl.HasChildren then
Return FindControl(ControlName, ctrl)
| End If
| Next ctrl
Return Nothing
| End Function

However some developer's prefer not to use the above, as they consider it
"multiple return points"...

Hope this helps
Jay

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
| Hi Everyone,
| I got the following code from Deborah Kurata's book. I need to return the
| control that matches the name that I pass into the function. It does
iterate
| through the form but it will not return a control reference. Following the
| code, it does actually get a reference, but when it returns from one of
the
| recursize calls, the code keeps going. For examle:
| First call to FinControl
| Has child so call FindControl again
| Finds control, returns the reference
| 'Comment I need a garnteed way of exiting now
| Keeps looping, and if a control has child again, call FindControl again
|
| Private Function FindControl( ByVal ControlName As String, ByVal
CurrentForm
| As control) As Control
| Dim rtn As Control
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname then
| rtn = ctrl
| exit For
| End If
| If ctrl.HasChildren then
| FindControl(ControlName, ctrl)
| End If
| Next ctrl
| return rtn
| End Function
|
| What is the best way to handle this one. Thanks for any info.
| Michael
|
Nov 21 '05 #2
I think that Jay might have got it slightly wrong.

In the original procedure, if the target control was 'top level' then I
would expect the procedure to find it and return a reference to it:

For Each ctrl As Control In CurrentForm.Controls
If ctrl.Name = Controlname Then
rtn = ctrl
Exit For
End If
...
Next ctrl
Return rtn

However, once a recursive call is made, the result is being 'thrown away',
the loop will continue and the symptoms as described will be exhibited:

...
If ctrl.HasChildren Then
FindControl(ControlName, ctrl) <--- The result from this call is not
assigned to anything
End If

My take on it is:

Private Function FindControl( ByVal ControlName As String, ByVal
CurrentForm As control) As Control

Dim rtn As Control

For Each ctrl As Control In CurrentForm.Controls
If ctrl.Name = Controlname Then
rtn = ctrl
Exit For
End If
If ctrl.HasChildren Then
rtn = FindControl(ControlName, ctrl)
If Not (rtn Is Nothing) then Exit For
End If
Next ctrl

Return rtn

End Function

The check for Not Nothing is important otherwise the loop will continue to
the end of the 'children', control will drop through the end of the loop and
Nothing will be returned regardless of whether we found it or not. The early
exit when we find it in a recursive call corrects this oversight.
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:et**************@TK2MSFTNGP10.phx.gbl...
Michael,
I would expect:

| If ctrl.HasChildren then
rtn = FindControl(ControlName, ctrl)
Exit For
| End If

However I would not define the rtn variable, I would simply use the Return
statement when I found an item. Something like:

| Private Function FindControl( ByVal ControlName As String, ByVal
CurrentForm
| As control) As Control
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname then
Return ctrl
| End If
| If ctrl.HasChildren then
Return FindControl(ControlName, ctrl)
| End If
| Next ctrl
Return Nothing
| End Function

However some developer's prefer not to use the above, as they consider it
"multiple return points"...

Hope this helps
Jay

"Michael" <Mi*****@discussions.microsoft.com> wrote in message
news:7E**********************************@microsof t.com...
| Hi Everyone,
| I got the following code from Deborah Kurata's book. I need to return
the
| control that matches the name that I pass into the function. It does
iterate
| through the form but it will not return a control reference. Following
the
| code, it does actually get a reference, but when it returns from one of
the
| recursize calls, the code keeps going. For examle:
| First call to FinControl
| Has child so call FindControl again
| Finds control, returns the reference
| 'Comment I need a garnteed way of exiting now
| Keeps looping, and if a control has child again, call FindControl again
|
| Private Function FindControl( ByVal ControlName As String, ByVal
CurrentForm
| As control) As Control
| Dim rtn As Control
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname then
| rtn = ctrl
| exit For
| End If
| If ctrl.HasChildren then
| FindControl(ControlName, ctrl)
| End If
| Next ctrl
| return rtn
| End Function
|
| What is the best way to handle this one. Thanks for any info.
| Michael
|

Nov 21 '05 #3
Michael,

I am fond of those recursive ones. An alternative

\\\\
Private Function ControlName(ByVal parentCtr As Control, _
ByVal NameString As String) As Control
If ControlName Is Nothing Then
Dim ctr As Control
For Each ctr In parentCtr.Controls
If ctr.Name = NameString Then
Return ctr
Else
Dim getControl As Control = ControlName(ctr, NameString)
If Not getControl Is Nothing Then Return getControl
End If
Next
Else
Return ControlName
End If
End Function
///

I don't say this one is better.

Cor
Nov 21 '05 #4
"Michael" <Mi*****@discussions.microsoft.com> schrieb:
I got the following code from Deborah Kurata's book. I need to return the
control that matches the name that I pass into the function. It does
iterate
through the form but it will not return a control reference. Following the
code, it does actually get a reference, but when it returns from one of
the
recursize calls, the code keeps going.


Accessing controls by their names or indices
<URL:http://dotnet.mvps.org/dotnet/faqs/?id=controlbynameindex&lang=en>

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5
Herfried,

The chalenge is here the escape when found.

:-)

Cor
Nov 21 '05 #6
"Cor Ligthert" <no************@planet.nl> schrieb:
The chalenge is here the escape when found.


ACK.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #7
Stephany,
| rtn = FindControl(ControlName, ctrl)
| If Not (rtn Is Nothing) then Exit For

That makes more sense... It was one of those answers I didn't verify first
;-)

Thanks
Jay
"Stephany Young" <noone@localhost> wrote in message
news:uC**************@TK2MSFTNGP10.phx.gbl...
|I think that Jay might have got it slightly wrong.
|
| In the original procedure, if the target control was 'top level' then I
| would expect the procedure to find it and return a reference to it:
|
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname Then
| rtn = ctrl
| Exit For
| End If
| ...
| Next ctrl
| Return rtn
|
| However, once a recursive call is made, the result is being 'thrown away',
| the loop will continue and the symptoms as described will be exhibited:
|
| ...
| If ctrl.HasChildren Then
| FindControl(ControlName, ctrl) <--- The result from this call is not
| assigned to anything
| End If
|
| My take on it is:
|
| Private Function FindControl( ByVal ControlName As String, ByVal
| CurrentForm As control) As Control
|
| Dim rtn As Control
|
| For Each ctrl As Control In CurrentForm.Controls
| If ctrl.Name = Controlname Then
| rtn = ctrl
| Exit For
| End If
| If ctrl.HasChildren Then
| rtn = FindControl(ControlName, ctrl)
| If Not (rtn Is Nothing) then Exit For
| End If
| Next ctrl
|
| Return rtn
|
| End Function
|
| The check for Not Nothing is important otherwise the loop will continue to
| the end of the 'children', control will drop through the end of the loop
and
| Nothing will be returned regardless of whether we found it or not. The
early
| exit when we find it in a recursive call corrects this oversight.
|
|
| "Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
| news:et**************@TK2MSFTNGP10.phx.gbl...
| > Michael,
| > I would expect:
| >
| > | If ctrl.HasChildren then
| > rtn = FindControl(ControlName, ctrl)
| > Exit For
| > | End If
| >
| > However I would not define the rtn variable, I would simply use the
Return
| > statement when I found an item. Something like:
| >
| > | Private Function FindControl( ByVal ControlName As String, ByVal
| > CurrentForm
| > | As control) As Control
| > | For Each ctrl As Control In CurrentForm.Controls
| > | If ctrl.Name = Controlname then
| > Return ctrl
| > | End If
| > | If ctrl.HasChildren then
| > Return FindControl(ControlName, ctrl)
| > | End If
| > | Next ctrl
| > Return Nothing
| > | End Function
| >
| > However some developer's prefer not to use the above, as they consider
it
| > "multiple return points"...
| >
| > Hope this helps
| > Jay
| >
| > "Michael" <Mi*****@discussions.microsoft.com> wrote in message
| > news:7E**********************************@microsof t.com...
| > | Hi Everyone,
| > | I got the following code from Deborah Kurata's book. I need to return
| > the
| > | control that matches the name that I pass into the function. It does
| > iterate
| > | through the form but it will not return a control reference. Following
| > the
| > | code, it does actually get a reference, but when it returns from one
of
| > the
| > | recursize calls, the code keeps going. For examle:
| > | First call to FinControl
| > | Has child so call FindControl again
| > | Finds control, returns the reference
| > | 'Comment I need a garnteed way of exiting now
| > | Keeps looping, and if a control has child again, call FindControl
again
| > |
| > | Private Function FindControl( ByVal ControlName As String, ByVal
| > CurrentForm
| > | As control) As Control
| > | Dim rtn As Control
| > | For Each ctrl As Control In CurrentForm.Controls
| > | If ctrl.Name = Controlname then
| > | rtn = ctrl
| > | exit For
| > | End If
| > | If ctrl.HasChildren then
| > | FindControl(ControlName, ctrl)
| > | End If
| > | Next ctrl
| > | return rtn
| > | End Function
| > |
| > | What is the best way to handle this one. Thanks for any info.
| > | Michael
| > |
| >
| >
|
|
Nov 21 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Magnus Lie Hetland | last post by:
Hi! I've been looking at ways of dealing with nested structures in regexps (becuase I figured that would be faster than the Python parsing code I've currently got) and came across a few...
9
by: peter | last post by:
Hello all, Recently I've started to refactor my code ...(I'm using python 2.3.4) I tried to add extra functionality to old functions non-intrusively. When I used a construct, which involves...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
7
by: Aloo | last post by:
Dear friends, If we declare a recursive function as 'inline' then does it actually convert to an iterative form during compilation ( the executable code is iterative)? Is it possible ? ...
9
by: Csaba Gabor | last post by:
Inside a function, I'd like to know the call stack. By this I mean that I'd like to know the function that called this one, that one's caller and so on. So I thought to do: <script...
41
by: Harry | last post by:
Hi all, 1)I need your help to solve a problem. I have a function whose prototype is int reclen(char *) This function has to find the length of the string passed to it.But the conditions...
5
by: Digital Puer | last post by:
I got this on an interview: Is it possible to write inline recursive functions? I said yes, but there is no guarantee that the compiler will definitely inline your code even if you write...
2
by: Andrea Taverna | last post by:
Hello everyone, I wrote a bunch of recursive functions to operate on multi-dimensional matrices. The matrices are allocated dynamically in a non-contiguous way, i.e. as an array of pointers...
6
KevinADC
by: KevinADC | last post by:
This snippet of code provides several examples of programming techniques that can be applied to most programs. using hashes to create unique results static variable recursive function...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.