The project I'm currently working on requires me to create a tree data structure. Below is the sample how I've tried to achieve this functionality. I've decided to create a child node collection as a nested class since it allows me to set Nodes parent in it's Add() method while keeping parent setter private, so that classes derived from Node or other classes in the same assembly couldn't get direct access to it.
class Node<T> where T : Node<T>
{
private T mParent;
private ChildNodeCollection<T> mChildren;
public T Parent
{
get{return this.InnerParent;}
}
private T InnerParent
{
get{return this.mParent;}
set {this.mParent = value;}
}
public Node()
{
this.mChildren = new ChildNodeCollection<T>(this);
}
class ChildNodeCollection<U> where U : T
{
private U mParent;
public U CollectionParent
{
get{return this.mParent;}
}
public ChildNodeCollection(U parent)
{
this.mParent = parent;
}
public void Add(U item)
{
item.InnerParent = this.CollectionParent;
...
}
}
}
This code doesn't compile though. It complains about this.mChildren = new ChildNodeCollection(this) line in the Node constructor. It throws these two errors.
Error 35 The best overloaded method match for Node<T>.ChildNodeColllection<T>.ChildNodeColllection(T)' has some invalid arguments
Error 36 Argument '1': cannot convert from Node<T> to T
I guess it can't work out that T is Node, even though I specified so in the class definition. I'm curious if anybody has any ideas how this could be done differently in a way that would allow me to set Node's parent when adding it to collection without exposing Node's Parent property too much with internal access modifier.