473,395 Members | 1,504 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,395 software developers and data experts.

short circuit a recursive function

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
Apr 23 '07 #1
8 1418
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:O5**************@TK2MSFTNGP03.phx.gbl...
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.
>
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

Apr 23 '07 #2
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

"Michael C" <no****@nospam.comwrote in message
news:O8**************@TK2MSFTNGP02.phx.gbl...
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:O5**************@TK2MSFTNGP03.phx.gbl...
>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.
>>
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


Apr 24 '07 #3
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:eF**************@TK2MSFTNGP04.phx.gbl...
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
Apr 24 '07 #4
yes, that was it!
thanks very much Michael.

"Michael C" <no****@nospam.comwrote in message
news:OR**************@TK2MSFTNGP03.phx.gbl...
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:eF**************@TK2MSFTNGP04.phx.gbl...
>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

Apr 24 '07 #5
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
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
Apr 24 '07 #6

"Michael C" <no****@nospam.comwrote in message
news:OH*************@TK2MSFTNGP06.phx.gbl...
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>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
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.
>
Michael

Apr 24 '07 #7
>
If (ctrl.GetType Is GetType(DataGridView))

can be just

If ctrl is DataGridView
This does work:

If TypeOf ctrl Is DataGridView
Apr 24 '07 #8
"Rick" <Ri**@LakeValleySeed.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
>
>>
If (ctrl.GetType Is GetType(DataGridView))

can be just

If ctrl is DataGridView

This does work:

If TypeOf ctrl Is DataGridView
That's the one. I'm a bit rusty on vb.
>

Apr 27 '07 #9

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

Similar topics

16
by: Dave Opstad | last post by:
In this snippet: d = {'x': 1} value = d.get('x', bigscaryfunction()) the bigscaryfunction is always called, even though 'x' is a valid key. Is there a "short-circuit" version of get that...
3
by: Steven Wong | last post by:
Just wondering, is it portable to write: something* pSomething = getSomething(); if( pSomething && pSomething->stuff() ) { .... }
2
by: webposter | last post by:
Hi, I am looking for information on a data structure (and associated algorithm) to do short-circuit evaluation of boolean expressions and haven't found a single one even after googing for two...
5
by: Michael Jørgensen | last post by:
Hi there, Is there any difference between bool success = SomeFunctionReturningFalse(); success &= SomeOtherFunction(); and bool success = SomeFunctionReturningFalse();
5
by: Phil Jones | last post by:
I'm just (as a matter of course) using AndAlso and OrElse statements to short circuit checking, even for nominal things like Boolean comparisons. I'm wondering, is that a good thing to do (for...
6
by: James Curran | last post by:
In recent weeks, I've twice come upon people who consider the short- curcuited evaluation of logical && (and ||) to be just a vendor- defined compiler quirk, and recommend against depending on it....
0
by: Greg Nash | last post by:
G'day I have a before-update trigger a bit like .... REFERENCING OLD AS O NEW AS N .... WHEN (N.STATUS 0 AND EXISTS (SELECT EXTREF FROM MY.ET WHERE EXTREF=N.REF OR EXTREF=O.REF))
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
30
by: Lassie | last post by:
bool finished = is_done(); if (finished && try_again()) { } Is my understanding of short circuit evaluation correct that if finished is false that try_again() will never be executed? Or does...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.