• Advertisement
Sign in to follow this  

VB .NET recursion

This topic is 4069 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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,

Share this post


Link to post
Share on other sites
Advertisement
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 Boolean

exitRecursion = False

Private 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



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement