3

I'm trying to use TreeNodes to create a Tree like structure in code. I'm not all that familiar with TreeNodes at all. I did some searching beforehand but I still don't feel like I understand exactly what I am doing.

I am creating a Game in Unity using C#. I'm using XML to create dialogue and I want to store the different options from the different choices in a Tree-like structure.

A visual representation of this would be like:

-------------------------------choice a --------------------------- choice b --------------------------------

                /--------|--------\          /--------|--------\

            choice d  choice e  choice f choice g  choice h  choice i

and so on.

public class TreeNode<T> : IEnumerable<TreeNode<T>> 
{
    public T Data {get; set;}
    public TreeNode<T> Parent { get; set; }
    public ICollection<TreeNode<T>> Children {get; set;}

    public TreeNode(T data) {
        this.Data = data;
        this.Children = new LinkedList<TreeNode<T>>();
    }

    public TreeNode<T> AddChild(T child) {
        TreeNode<T> childNode = new TreeNode<T>(child) {Parent = this};
        this.Children.Add (childNode);
        return childNode;
    }
}

Currently I am getting the error TreeNode<T>' does not implement interface memberSystem.Collections.Generic.IEnumerable>.GetEnumerator()'.

I'm not entirely sure what that error even means, any help would be appreciated.

My first time posing a question in StackOverFlow so if this is in the wrong spot, please let me know.

Thanks

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35

2 Answers2

0

The issue is in the line where you declare that TreeNode<T> (will) implement IEnumerable<TreeNode<T>>. In order to properly implement that interface, the class needs to have an implementation of all the methods the interface exposes.

public class TreeNode<T> : IEnumerable<TreeNode<T>> 

Remove : IEnumerable<TreeNode<T>> and it'll compile correctly. Unless of course you want to implement that interface, then you need to add implementations of all the IEnumerable methods.

gnalck
  • 972
  • 6
  • 12
0

You can either try removing the inherited : IEnumerable<TreeNode<T>> or you would need to implement the System.Collections.IEnumerable.GetEnumerator() method.

See this thread for some extra reading on the error, and possibly some further understanding.

Quoted "You did not implement System.Collections.IEnumerable.GetEnumerator. When you implement the generic IEnumerable you have to also implement System.Collections.IEnumerable's GetEnumerator."

Community
  • 1
  • 1
Hexie
  • 3,955
  • 6
  • 32
  • 55