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
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
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
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.
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