implementing IHierarchicalEnumerable, how?

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

hi

I have a generic collection I want to display inside a TreeView control

This is how my class look like:
public class Comment
{
private Guid _id;
private Guid _userid;
private Guid _article;
private Guid _parent;
private List<Comment> _replies;

public comment() {}
}

from what I understand I have to implement the IHierarchicalEnumerable
interface:

So I started with implementing the GetHierarchyData method:
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}

But I'm unsure how to implement the GetEnumerator method :(

How should I implement GetEnumerator and is GetHierarchyData implemented
correctly?
 
you first need to implement IHierarchyData in your Comment class. then change
_replies to a collection based on List<> that implements
IHierarchicalEnumerable:

....
private CommentCollection _replies;
....

public class CommentCollection : List<Comment>, IHierarchicalEnumerable
{
public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}
}

-- bruce (sqlwork.com)
 
Hi

Below you see the changes I made to the Comment class, Someting is wrong in
my code, I get these errors:
- "does not implement interface member
'System.Collections.IEnumerable.GetEnumerator()"
- "Comment.GetEnumerator()' cannot implement
'System.Collections.IEnumerable.GetEnumerator()'
because it does not have the matching return type of
'System.Collections.IEnumerator"

public class Comment : IHierarchicalEnumerable
{
private Guid _id;
private Guid _userid;
private Guid _article;
private Guid _parent;
private CommentCollection _replies;

public comment() {}

public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}

public IEnumerator<Comment> GetEnumerator()
{
return new Enumerator<Comment>(this);
}

}


public class CommentCollection : List<Comment>, IHierarchicalEnumerable
{
public CommentCollection() : base() {}

public IHierarchyData GetHierarchyData(object enumeratedItem)
{
return enumeratedItem as IHierarchyData;
}
}

any suggestions?
 
Back
Top