• Create Account

## VB .NET recursion

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

### #1matt_stevens_2006  Members

Posted 04 December 2006 - 12:50 AM

Hello I have a question about some recursion and something im experiencing in my debugging as well as a result. I am just trying to understand this structure. Recursion gives me problems :( This is more pseudocodish because my code is pretty long. The problem is that when I exit sub its re-entering the function again in debug mode. I know that I have entered a treeNode so the recTreeView function has in fact been called recursively once. Is this because each call to this function is stacked and 'exit sub' only exits the current subfolder?? My thought is that since my call history is RecTreeView(ByVal pObject As Statute) RecTreeView(ByVal pObject As Statute) --->EXIT SUB that its jumping up RecTreeView(ByVal pObject As Statute) <----to here and reentering RecTreeView(ByVal pObject As Statute) Maybe I am not understanding how recursion works in VB
Private Sub RecTreeView(ByVal pObject As Statute)
For i = 0 To Me.TreeView.SelectedNode.GetNodeCount(False) - 1
If TreeView.SelectedNode.IsFolder Then
Me.TreeView.SelectedNode = Me.TreeView.SelectedNode.Nodes(i)
Me.RecTreeView(pObject)
ElseIf SelectedNode.ID = pObject.ID
Me.TreeView.SelectedNode = Me.TreeView.SelectedNode.Nodes(i)
Exit Sub
End If
Next


If this post isnt too clear I apologize in advance im having trouble with this. Thanks for your time,

### #2Dim_Yimma_H  Members

Posted 04 December 2006 - 01:59 AM

You're right about the calls being stacked, in the call stack memory. That's the way it works in the most common programming languages, each function call is stored after the latest call on the stack, and becomes the new "latest". When a function exits only the latest call is removed from the stack, and the previous call becomes the "latest".

To exit the whole recursion stack you need to set a variable to an exit state, and the recursive function checks this state if it's allowed to continue calling recursively. If it's not allowed then it exits, and the previous function call keeps checking the same until reaching the original function, which isn't recursive.

I added some to your code, assuming the goal is to find ID, it was some time since I used VB so think of it as pseudo code [grin]

Private exitRecursion As BooleanexitRecursion = FalsePrivate Sub RecTreeView(ByVal pObject As Statute)        For i = 0 To Me.TreeView.SelectedNode.GetNodeCount(False) - 1            'keep exiting if ID has been found            If exitRecursion = True Then                Exit Sub            End If            If TreeView.SelectedNode.IsFolder Then                Me.TreeView.SelectedNode = Me.TreeView.SelectedNode.Nodes(i)                Me.RecTreeView(pObject)            ElseIf SelectedNode.ID = pObject.ID                Me.TreeView.SelectedNode = Me.TreeView.SelectedNode.Nodes(i)                'set state variable to exit recursion since ID has been found		exitRecursion = True            End If        Next

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.