c# - How to Create a Class That Can Have Parent and Child Relationship -


i have seen quite few articles on here question none answer asking. creating class of branch objects can envision treenode objects of treeview control. each branch can have number of branch children below (and therefore above) it. here rather simple class:

public class branch {     public string name { get; set; }     public string link { get; set; }     public branch parent { get; private set; }     public list<branch> children { get; set; }      internal branch(string name, string link) {         this.name = name;         this.link = link;         this.children = new list<branch>();     } // branch - constructor - overload      internal branch(string name, string link, list<branch> children) {         this.name = name;         this.link = link;         this.children = children;          this.children.foreach(delegate(branch branch) {             branch.parent = this;         });     } // branch - constructor - overload      public bool haschildren {         { return this.children.count > 0; }     } // haschildren - property - readonly      public string path {         {             string result = "";              branch parent = this;             while (parent != null) {                 result = string.format("{0}/{1}", parent.name, result);                 parent = parent.parent;             } // while stepping tree              return string.isnullorwhitespace(result) ? "" : result.substring(0, result.length - 1);         } //     } // path - property - readonly 

this works great if add children @ time of instantiation following:

list<branch> branches = new list<branch>() {     new branch("first", "#"),     new branch("second", "#"),     new branch("third", "#", new list<branch>() {         new branch("thirdsub1", "#"),         new branch("thirdsub2", "#")     }),     new branch("fourth", "#"),     new branch("fifth", "#"),     new branch("sixth", "#", new list<branch>() {         new branch("sixthsub1", "#"),         new branch("sixthsub2", "#", new list<branch>() {             new branch("sixthsub2sub1", "#"),             new branch("sixthsub2sub2", "#"),             new branch("sixthsub2sub3", "#", new list<branch>() {                 new branch("deep deep deep undercover", "#"),             }),         }),     }),     new branch("seventh", "#"),     new branch("eighth", "#"), }; 

but if following:

list<branch> branches = new list<branch>(); branch test = branches.add(new branch("something", "")); test.children.add(new branch("child here", "")); 

the "child here" node not have parent associated it. broken , of course path property doesn't work property.

i thought override list's add method not allowed. best way handle this? not creating own collection class mybranches, like, if there way of doing need while implementing ilist or iset or collection, willing so. please provide example.

thanks!

just people in future looking same solution, here full class:

public class branch {     public string name { get; set; }     public string link { get; set; }     public branch parent { get; set; }     public treebranches children { get; private set; }      internal branch(string name, string link) {         this.name = name;         this.link = link;         this.children = new treebranches(this);     } // branch - constructor - overload      internal branch(string name, string link, treebranches children) {         this.name = name;         this.link = link;         this.children = children;          this.children.tolist().foreach(delegate(branch branch) {             branch.parent = this;         });     } // branch - constructor - overload      /// <summary>     /// returns boolean indicating if given branch has child branches.     /// </summary>     public bool haschildren {         { return this.children.count > 0; }     } // haschildren - property - readonly      /// <summary>     /// gets path oldest ancestor current branch.     /// </summary>     public string path {         {             string result = "";              branch parent = this;             while (parent != null) {                 result = string.format("{0}/{1}", parent.name, result);                 parent = parent.parent;             } // while stepping tree              return string.isnullorwhitespace(result) ? "" : result.substring(0, result.length - 1);         } //     } // path - property - readonly  } // branch - class  public class treebranches : ilist<branch> {     private list<branch> branches = new list<branch>();     private branch owner;      public treebranches() {         this.owner = null;     }      public treebranches(branch owner) {         this.owner = owner;     }      public void add(branch branch) {         branch.parent = this.owner;         this.branches.add(branch);     }      #region standard ilist method implementation      ienumerator<branch> ienumerable<branch>.getenumerator() { return this.branches.getenumerator(); }     system.collections.ienumerator system.collections.ienumerable.getenumerator() { return this.branches.getenumerator(); }     public int indexof(branch item) { return this.branches.indexof(item); }     public void insert(int index, branch item) { this.branches.insert(index, item); }     public void removeat(int index) { this.branches.removeat(index); }     public branch this[int index] {         { return this.branches[index]; }         set { this.branches[index] = value; }     }      public void clear() { this.branches.clear(); }     public bool contains(branch item) { return this.branches.contains(item); }     public void copyto(branch[] array, int arrayindex) { this.branches.copyto(array, arrayindex); }     public int count { { return this.branches.count(); } }     public bool isreadonly { { return this.isreadonly; } }     public bool remove(branch item) { return this.branches.remove(item); }      #endregion standard ilist method implementation } // treebranches - class 

Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -