Solved

Rewrite method to recursive

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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

738 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