Solved

Rewrite method to recursive

Posted on 2011-03-02
3
432 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
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Asim Nazir earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
You can define them globally, normal or static.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
asp.net mvc5 3 35
C# Export DataGridView 4 37
Is IHttpActionResult a promise pattern? 2 32
c# if statement weird reaction 3 28
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now