Solved

Rewrite method to recursive

Posted on 2011-03-02
3
437 Views
Last Modified: 2012-05-11
I have a method that will look for child nodes.

            foreach (NodeElement elm in srv)
            {
                nodes = client.BrowseNodes(sd, elm.ItemId, daf);
                Console.WriteLine("\nNodes: ({0})", nodes.Count());

                Console.WriteLine("{0} = {1} and options({2}, {3}, {4})\n", elm.Name, elm.ItemId, elm.IsBranch ? true : false, elm.IsLeaf ? true : false, elm.HasChildren ? true : false);

                if (elm.HasChildren)
                {
                    foreach (NodeElement elm2 in nodes.Values)
                    {
                        nodes = client.BrowseNodes(sd, elm2.ItemId, daf);
                        Console.WriteLine("Sub(1) Nodes: " + nodes.Count());
                        Console.WriteLine("{0} = {1} and options({2}, {3}, {4})\n", elm2.Name, elm2.ItemId, elm2.IsBranch ? true : false, elm2.IsLeaf ? true : false, elm2.HasChildren ? true : false);
                    }
                }
            }


the problem is, as you can see, this can go maaaany ways deep. How do I best refactor this method to take xx levels? I think a recursive method who calls itself with current/next node level is the way to do the trick.
0
Comment
Question by:dingir
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 500 total points
ID: 35015857
Here it's

 
private void RecursiveLoop(nodeElement srv)
{
foreach (NodeElement elm in srv)
            {
                nodes = client.BrowseNodes(sd, elm.ItemId, daf);
                Console.WriteLine("\nNodes: ({0})", nodes.Count());

                Console.WriteLine("{0} = {1} and options({2}, {3}, {4})\n", elm.Name, elm.ItemId, elm.IsBranch ? true : false, elm.IsLeaf ? true : false, elm.HasChildren ? true : false);

                if (elm.HasChildren)
                {
                    RecursiveLoop(elm); // Recall above method which will keep on process untill elm has no child
                }
            }
}

Open in new window


ASim
0
 
LVL 1

Author Comment

by:dingir
ID: 35016018
I also had to put in ref sd, ref daf, ref client also. Would there be any best practice of putting them into a class and call the recursive-method with one parameter instead?
0
 
LVL 10

Expert Comment

by:Asim Nazir
ID: 35016234
You can define them globally, normal or static.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question