short circuit a recursive function

R

Rick

I have the recursive function below which I use to find a datagridview with
a certain bindingsource.

When the grid is parented by a control with only one child (the dgv) the
function works ok, but when there are multiple controls the function calls
itself and eventually returns "Nothing" if the dgv is not the last control.

How can I short circuit this function so that once it finds my target it
exits for good? Do I need some other kind of function and a recursive one?

Rick


Private Function FindDGV(ByVal parent As Control) As DataGridView
Dim dgv As DataGridView
FindDGV = dgv

Dim ctrl As Control

For Each ctrl In parent.Controls
If (ctrl.GetType Is GetType(DataGridView)) AndAlso _
DirectCast(ctrl,
DataGridView).DataSource.Equals(Nav.BindingSource) Then
Return DirectCast(ctrl, DataGridView)
Else
If ctrl.Controls.Count > 0 Then FindDGV(ctrl)
End If

End Function
 
M

Michael C

Rick said:
How can I short circuit this function so that once it finds my target it
exits for good? Do I need some other kind of function and a recursive
one?

This type of function is fine. The only problem I can see if the Else part
of the function is missing the return keyword on the call to FindDGV.
 
R

Rick

Michael,

I don't understand what you mean, can you change my code to show me?

What I think is happening is this: If I get to a splitter control with my
dgv in Panel1 I get a sucessful return from the function, but then it
continues to look in panel2 where it does not find anything and finally
returns nothing.

Rick
 
M

Michael C

Rick said:
Michael,

I don't understand what you mean, can you change my code to show me?

What I think is happening is this: If I get to a splitter control with my
dgv in Panel1 I get a sucessful return from the function, but then it
continues to look in panel2 where it does not find anything and finally
returns nothing.

It returns nothing because you're missing the return keyword:

If ctrl.Controls.Count > 0 Then return FindDGV(ctrl)

Michael
 
R

Rick

yes, that was it!
thanks very much Michael.

Michael C said:
It returns nothing because you're missing the return keyword:

If ctrl.Controls.Count > 0 Then return FindDGV(ctrl)

Michael
 
M

Michael C

Rick said:
yes, that was it!
thanks very much Michael.

No worries. BTW, I think this

If (ctrl.GetType Is GetType(DataGridView))

can be just

If ctrl is DataGridView

and this line can be deleted altogether:
FindDGV = dgv

Michael
 
R

Rick

Michael C said:
No worries. BTW, I think this

If (ctrl.GetType Is GetType(DataGridView))

can be just

If ctrl is DataGridView

seems not : VB.2005 compiler reports - 'DataGridView' is a type and cannot
be used as an expression
and this line can be deleted altogether:
FindDGV = dgv

yes, agreed. I do this reflexively in functions even though in this case a
return value of nothing is ok.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top