Results 1 to 5 of 5

Thread: Recursive method help

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Aug 2003
    Location
    Edinburgh, UK
    Posts
    2,773

    Recursive method help

    I am trying to go through each and every single treenode in a treeview.

    I want to be able to return a treenode, or null, if a specific object has been found. how can I do this? I feel like i've lost all my knowledge!


    private TreeNode GetObjectFromTreeView(TreeNode startingNode, MyObject objectToMatch)
    {
    foreach(TreeNode currentNode in startingNode.Nodes)
    {
    if (currentNode.Tag != null && currentNode.Tag.GetType() == typeof(MyObject))
    {
    return currentNode;
    }

    return this.GetObjectFromTreeView(currentNode, objectToMatch);

    }


    return null;
    }

    MVP 2007-2010 any chance of a regain?
    Professional Software Developer and Infrastructure Engineer.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: Recursive method help

    You're checking the type of the object (which is sensible), but not its value. If you have more than one tag of the same type then you'll simply stop at the first one, by the look of it.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Recursive method help

    Easier than using GetType:
    Code:
    currentNode.Tag is MyObject

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Recursive method help

    Actually, you don't need to check its type: just compare references.
    Code:
    if (currentNode.Tag != null && Object.ReferenceEquals(currentNode.Tag, objectToMatch))

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Recursive method help

    CSharp Code:
    1. private TreeNode GetObjectFromTreeView(TreeNode startingNode, MyObject objectToMatch)
    2. {
    3.     TreeNode result = null;
    4.  
    5.     foreach (TreeNode currentNode in startingNode.Nodes)
    6.     {
    7.         if (currentNode.Tag == objectToMatch)
    8.         {
    9.             result = currentNode;
    10.             break;
    11.         }
    12.  
    13.         result = this.GetObjectFromTreeView(currentNode, objectToMatch);
    14.  
    15.         if (result != null)
    16.         {
    17.             break;
    18.         }
    19.     }
    20.  
    21.     return result;
    22. }
    That code will not work for value types but it will for reference types.
    Last edited by jmcilhinney; Dec 13th, 2007 at 06:28 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width