Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Rewrite method to recursive

Posted on 2011-03-02
3
Medium Priority
?
438 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

704 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