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
Medium Priority
438 Views
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
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
• 2

LVL 10

Accepted Solution

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
}
}
}
``````

ASim
0

LVL 1

Author Comment

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

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

## Featured Post

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
Course of the Month6 days, 18 hours left to enroll