Click here to Skip to main content
15,741,892 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am working on projects which requires me to represent data in a tree format, I have tried iterating through objects, but facing trouble accessing and assigning data to inner objects, the main class looks like this

C#
public class Node
{
    public string Name { get; set; }
    public List<Node> Child { get; set; }
    public Node()
    {
        this.Child = new List<Node>();
    }
}

and there is a function which will take List and create Tree of Node class, by iterating through the list
C#
public Node NodeLister(myclass value)
    {
        List<Node> holder = new List<Node>();
        Node head = new Node();
        Node temp;
        int i = 0;
        foreach (MenuElements item in value.value)
        {
            if (i == 0)
            {
               head =new Node { Name = item.Name };
            }
            else
            {
                temp = new Node { Name = item.Name };
                holder.Add(temp);
                holder = holder[holder.IndexOf(temp)].Child;
            }
            i++;
        }
        head.Child = holder;
        return head;
     }

the part
C#
holder = holder[holder.IndexOf(temp)].Child;

has to somehow have pointer kind of effect as this statement is directly assigning the value and erasing the previous data rather than iterating through it. myclass Class looks something like below.

C#
public class myclass
       {

           public List<MenuElements> value { get; set; }
       }

       public class MenuElements
       {

           public string Name { get; set; }

       }

I know that this will not create a tree but solving this issue will help me moving forward, and creating the tree. any help would be appreciated
Posted
Comments
Sergey Alexandrovich Kryukov 27-Dec-15 15:51pm    
Your code has a number of apparent problems. MenuElements is not defined. (A class should be named, according good Microsoft naming convention, a singular noun, say, MenuElement, MenuElementSet.) What is List<MenuElements>? List if set? :-). The class "myclass" looks redundant. If MenuElements is related to UI, your other problem is the lack of proper separation of UI from data.

Your Node constructor is bad, is not needed. The list should be initialized in-place. Node should be a generic class. You need to abstract out and encapsulate tree operations; it can be done in the Node class itself. Your NodeList looks gibberish. I have no idea what prevents you from traversing a tree. You need to start with formulation of what that traversing should do.

—SA
nanu146 27-Dec-15 23:38pm    
sorry for my bad naming practice, I will surely take care of my Naming conventions. Since, you seems to have an idea on this subject, can you please give me a rough logic of how to iterate through this structure. I thoroughly know how to iterate if I were using c++ using pointer concept, but I came to know that though pointer are available in c# it is not advisable, since CLR cannot create pointers to mannaged structures. Thanks for you time and support.
Sergey Alexandrovich Kryukov 28-Dec-15 0:02am    
First of all, the statement about CLR is total gibberish. CLI reference to a managed structure is complete analog of a pointer to the unmanaged. If a pointer to a managed structure existed, the structure could not be managed. And a pointer can exist, but temporarily, through pinning. And in C++ (unmanaged, not to talk about C++/CLI) also there is no a pointer to managed structure :-).

I can answer everything on traversing a tree structure, but I cannot understand what prevents you traversing it. What do you want to traverse? Visit all nodes? What's the problem? Only let's talk about a tree, not pointless "myclass". Just take a node, iterate through List. Each node has List, too, so iterate it, too.

—SA
BillWoodruff 27-Dec-15 23:17pm    
It appears that 'myclass and 'MenuElements are not hiearchic structures (nested), so why use a Tree structure to represent them ?
nanu146 27-Dec-15 23:29pm    
those contains list of menu elements sorted in a specific order,and this has to be represented in Tree by using Node class, Every myclass property value has data for one path of a whole tree, there would be many iterations of this property, with same entry node and different lateral nodes, thus making it tree structure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900