Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 244,303 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 762 people online right now. Registration is fast and FREE... Join Now!




Exiting from a Recursive Sub Procedure without finishing all iteration

 
Reply to this topicStart new topic

Exiting from a Recursive Sub Procedure without finishing all iteration

dranfu
2 Jan, 2009 - 07:36 PM
Post #1

New D.I.C Head
*

Joined: 24 Nov, 2008
Posts: 14

So, I've been applying some recursive techniques to the second version of my app. This recursive procedure simply iterates through all nodes in a tree view and tests whether the text in the "department text box" matches the text name of a particular node. If there is a match, then the user's name, stored in the "name" variable, is added as a node to the node that has the same name as the department text box text.

CODE


CallTestForMatch(TreeView1)

Private Sub TestForMatch(ByVal n As TreeNode)

      Dim aNode As TreeNode
      Dim building1 As String = CStr(building_ComboBox.SelectedItem)
      Dim department1 As String = CStr(department_ComboBox.SelectedItem)
      Dim name As String = ((last_name_TextBox.Text) & ", " & (first_name_TextBox.Text))
      If n.Text = department1 Then
         n.Nodes.Add(name)
         Exit Sub
      End If

      For Each aNode In n.Nodes
         TestForMatch(aNode)
      Next


   End Sub
   Private Sub CallTestForMatch(ByVal aTreeView As TreeView)
      Dim node As TreeNode
      For Each node In aTreeView.Nodes
         TestForMatch(node)
      Next
   End Sub



The only problem is, when a match is found, and the name variable is added to the TreeView as a new node, I want to Exit out of the recursive procedure with the Exit Sub statement. Unfortunately, it doesn't happen. Even when a match is found, the program keeps iterating through every single node until all nodes have been examined. Is there another Keyword I can use to exit the recursive procedure? Do I need to create another procedure to get out of the recursive one?

I'm lost right now.

User is offlineProfile CardPM
+Quote Post


Core
RE: Exiting From A Recursive Sub Procedure Without Finishing All Iteration
2 Jan, 2009 - 08:03 PM
Post #2

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
You can add a boolean variable to your code, for example isFound. Declare it right after Public Class FormName and initialize it as False:

CODE

Private isFound As Boolean = False


So, in the TestForMatch method, the verification statement would look like this:

CODE

If n.Text = department1 Then
         n.Nodes.Add(name)
         isFound = True
End If


Then, for the CallTestForMatch method you should use this code:

CODE

      Dim node As TreeNode
      For Each node In aTreeView.Nodes
         TestForMatch(node)
         If isFound = True
             Exit Sub
         End If
      Next


This post has been edited by Core: 2 Jan, 2009 - 08:03 PM
User is offlineProfile CardPM
+Quote Post

dranfu
RE: Exiting From A Recursive Sub Procedure Without Finishing All Iteration
2 Jan, 2009 - 08:33 PM
Post #3

New D.I.C Head
*

Joined: 24 Nov, 2008
Posts: 14

Ok, I will try that, and thanks! But why does it matter if I add the Boolean variable in the calling procedure, versus from the recursive TestForMatch method. Should not "Exit Sub" take me out of the Recursive TestForMatch Methods all together?


User is offlineProfile CardPM
+Quote Post

Core
RE: Exiting From A Recursive Sub Procedure Without Finishing All Iteration
2 Jan, 2009 - 08:41 PM
Post #4

using DIC.Mod.Core;
Group Icon

Joined: 8 Dec, 2008
Posts: 1,780



Thanked: 139 times
Dream Kudos: 350
Expert In: Software Development, Software Testing/Debugging

My Contributions
QUOTE

Should not "Exit Sub" take me out of the Recursive TestForMatch Methods all together?


It just exits the procedure in which it appears. The boolean value in this case will show the program that the needed value has already been found, so if it gets the True value, it will force the For loop to stop.
User is offlineProfile CardPM
+Quote Post

dranfu
RE: Exiting From A Recursive Sub Procedure Without Finishing All Iteration
2 Jan, 2009 - 11:32 PM
Post #5

New D.I.C Head
*

Joined: 24 Nov, 2008
Posts: 14

It didn't work. The method still iterates through every node of the tree, even with the boolean variable.

CODE


CallTestForMatch(TreeView1)


   End Sub ' e-inventory Save Button Click

   Private Sub TestForMatch(ByVal n As TreeNode)

      Dim aNode As TreeNode
      Dim building1 As String = CStr(building_ComboBox.SelectedItem)
      Dim department1 As String = CStr(department_ComboBox.SelectedItem)
      Dim name As String = ((last_name_TextBox.Text) & ", " & (first_name_TextBox.Text))
      If n.Text = department1 Then
         n.Nodes.Add(name)
         IsFound = True
         Exit Sub
      End If

      For Each aNode In n.Nodes
         TestForMatch(aNode)
      Next


   End Sub
   Private Sub CallTestForMatch(ByVal aTreeView As TreeView)
      Dim node As TreeNode
      For Each node In aTreeView.Nodes
         TestForMatch(node)
         If IsFound = True Then
            Exit Sub
         End If

      Next
   End Sub

End Class



User is offlineProfile CardPM
+Quote Post

dranfu
RE: Exiting From A Recursive Sub Procedure Without Finishing All Iteration
3 Jan, 2009 - 09:53 AM
Post #6

New D.I.C Head
*

Joined: 24 Nov, 2008
Posts: 14

Ok, I got it to work by including the If...Then boolean verification in both of the CallTestForMatch methond and the TestForMatch methond. An example of the working code:

CODE


   Private Sub TestForMatch(ByVal n As TreeNode)

      Dim aNode As TreeNode
      Dim building1 As String = CStr(building_ComboBox.SelectedItem)
      Dim department1 As String = CStr(department_ComboBox.SelectedItem)
      Dim name As String = ((last_name_TextBox.Text) & ", " & (first_name_TextBox.Text))
      If (n.Text = department1) OrElse (n.Text = building1) Then
         n.Nodes.Add(name)
         IsFound = True
         Exit Sub
      End If

      For Each aNode In n.Nodes
         If IsFound = True Then
            Exit Sub
         End If
         TestForMatch(aNode)
      Next


   End Sub
   Private Sub CallTestForMatch(ByVal aTreeView As TreeView)
      Dim node As TreeNode
      For Each node In aTreeView.Nodes
         TestForMatch(node)
         If IsFound = True Then
            Exit Sub
         End If

      Next
   End Sub

End Class



The If...Then needed to be included at both points where it was testing any nodes smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 7/4/09 05:48PM

Live VB.NET Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month