Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have to write a method to get data from a list . My class structure is as below

C#
Class MyNodeItem
{
 public List<MyNodeItem> MyNodeItemList
 {
      get;set;
 }
 public string Text
 {
      get;set;
 }
}

Class MyDataClass
{
  public List <MyNodeItem> MyNodeItems
  {
    get;set;
  }
  public void MyMethod()
  {
   this.MyNodeItems = new List<MyNodeItem>();
   // Here i have code to add items into the myNodeItems
   // it can have nodeitems inside node item
  } 

}

class Client
{
  public void MyClientMethod()
  { 
    MyDataClass obj = new MyDataClass();
    obj.MyMethod();   
  }
}



How shall i retrive the obj.MyNodeItems in Hierarchical manner.
I need to write a method to get the obj.MyNodeItems and store it in Hierarchical manner.
How shall i do that
thanks in advance
Posted
Comments
BobJanova 23-Aug-12 11:12am    
Your question is not clear. What do you actually want to do?
Sergey Alexandrovich Kryukov 23-Aug-12 16:36pm    
How a list can be hierarchical?
--SA

C#
public void Iterate(MyNodeItem node)
{
   string s=  node.Text; // whatever

    foreach(MyNodeItem item in node.MyNodeItems)
        Iterate(item);
}


This is just a classic case of how to recurse. If you need to know the parent node, just pass two nodes in, and for the second one, null is the root, otherwise it's the parent.
 
Share this answer
 
v2
Comments
BobJanova 23-Aug-12 11:10am    
MyNodeItem does contain a List<MyNodeItem>.
Christian Graus 23-Aug-12 11:12am    
Oh. Well, then, just iterate over it. I will ammend my answer
A typical hierarchical (i.e. tree structure) data model looks something like this (pseudo-code):

class Tree<T> {
 TreeNode<T> Root;
}

class TreeNode<T> {
 T Value;
 List<TreeNode<T>> Children;
 TreeNode<T> Parent; // being able to go 'up' is also useful
}


Without knowing what you actually want to do with it, I can't be more detailed right now.
 
Share this answer
 

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