3

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.

4
  • 1
    Hmmm, shouldnt you have Node<T> as a parent and a collection of Node<T> as children? And also shouldnt you have a generic constructor? Commented May 26, 2011 at 19:27
  • if the parent node doesn't have any real qualities that differentiate if from the children, then you could make the methods you want to expose between Nodes protected while keeping the general Node methods like getting the parent public Commented May 26, 2011 at 19:28
  • @FlyingStreudel Where exactly, could you be more specific? Btw, I'm using .NET 2, so any new features added to generics in later version can't be used. Commented May 26, 2011 at 19:32
  • See my answer for more detail, not 100% its compliant with 2.0 though Commented May 26, 2011 at 19:59

3 Answers 3

3

In any case, you need to explicitly specify the type argument when creating a generic object using constructor, so you need to write something like:

this.mChildren = new ChildNodeCollection<T>(this);

This won't work because the type of this is Node<T> and not T (which is what the constructor requires). I think the easiest way to fix it is to store the parent as Node<T> instead of using a generic parameter.

The relevant parts of code would look like this:

public Node() {
  this.mChildren = new ChildNodeCollection(this);
}

class ChildNodeCollection {
  private Node<T> mParent;

  public ChildNodeCollection(Node<T> parent) {
    this.mParent = parent;
  }
}

I guess that your original goal (with using the T : Node<T> constraing) was to use inheritance to define more specific types of nodes. Then you'd like to retreive children (or parents) statically typed as T (that is, your specific node type). I may be wrong, but I seriously doubt this can be expressed with .NET generics.

I think it is a lot easier to use Node<T> as a type representing a node that contains your value of type T instead of using inheritance.

Sign up to request clarification or add additional context in comments.

2 Comments

Hmm... but i still don't quite get it why it doest recognize Node<T> as T
I'll try chaning it to this.mChildren = new ChildNodeColllection<T>(this as T); once I'll get back home.
0

You're specifying a constraint which says that U must inherit from T.

When you try to do this.mChildren = new ChildNodeCollection(this) U is implicitly defined as the type of this (Node<T>). But T isn't defined.

Comments

0

I believe this could be solved with the use of protected

class Node<T> 
{
    protected Node<T> _parent;
    protected List<Node<T>> _children;

    protected T _value;

    protected Node() { }

    public Node(T value)
    {
        _parent = null;
        _value = value;
        _children = new List<Node<T>>();
    }

    public void AddChild(Node<T> child) 
    {            
        child._parent = this;
        _children.Add(child);
    }
}

class NaughtyNode : Node<int>
{
    //Naughty nodes dont have parents, only kids

    public NaughtyNode(int value)
    {
        _value = value;
        _children = new List<Node<T>>();
    }

    public void BeNaughty()
    {
        Node<int> victim = new Node<int>(1);
        victim._parent = this; //Does not work, cannot access
    }

    public void AddChild(NaughtyNode child)
    {
        _children.Add(child);
    }
}

The protected only allows code within Node<T> to access it. NaughtyNode cannot see a Node<T>'s _parent.

3 Comments

I think you're mixing something up here. Protected modifier gives access to all classes inherited from Node<T>, thus NaughtyNode can access _parent and change it. Internal gives access to all classes defined in the same assembly, so if NaughtyNode is defined in the same assembly as Node<T>, then NaughtyNode can change it's child collection without setting childrens _parent to null, which could lead to inconsistencies.
Yep, I totally am. You do not want to use internal. But as far as I know, a NaughtyNode cannot access a Node<T>'s _parent... Going to compile a test real quick...
Attempting to access the _parent of a Node<T> from a NaughtyNode yields: Cannot access protected member 'StackNotebook.Node<int>._parent' via a qualifier of type 'StackNotebook.Node<int>'; the qualifier must be of type 'StackNotebook.NaughtyNode'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.