Solved

Rewrite method to recursive

Posted on 2011-03-02
3
434 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
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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