C# – Inherited Generics Constructor C#

cconstructorgenericsmultiple-inheritance

Hi I'm trying to make a generic treenode. Here is the abstract generic class

public abstract class TreeNode<T>
{
    protected List<TreeNode<T>> _childNodes = new List<TreeNode<T>>();
    protected TreeNode<T> ParentNode;

    public T ObjectData { get; set; }

    public TreeNode( TreeNode<T> parent, T data)
    {
        ParentNode = parent;
        ObjectData = data;
    }    
}

It has a companion interface

interface TreeNodeOperations<T>
{
    //Adds child to tree node
    public abstract void AddChild<T>(T child);
    //Performs N-Tree search
    public abstract TreeNode<T> SeachChild<T>(T child);
}

What I'm trying to do is inherit from both of these:

public class FHXTreeNode<T>: TreeNode<T>, TreeNodeOperations<T> where T : ParserObject
{
    public FHXTreeNode(FHXTreeNode<T> parent, T data) ---> # **ERROR** #
    {
        ParentNode = parent;
        ObjectData = data;
    }

    //Adds child to tree node
    public override FHXTreeNode<T> AddChild<ParserObject>(T childData)
    {
        FHXTreeNode<T> child = new FHXTreeNode<T>(this, childData);

        //_childNodes.Add(child);        

        return child;
    }

}

The error is: 'Parser.Objects.TreeNode' does not contain a constructor that takes 0 arguments

Help Pls!

Best Answer

You need to add a call to the base class' constructor.

And, subsequently, you don't need to set the properties inside of FHXTreeNode's constructor since it's handled in the base class' constructor. Your new constructor should look like this:

public FHXTreeNode(FHXTreeNode<T> parent, T data) : base(parent, data)
{
}