moving to next record in a Generic Collection

J

Jeff

hi

..NET 2.0

I have posted some code below which I'm using at a webportal I'm trying to
develop. I have problems with this code at moment:

From what I understand from reading this text it assume that there is only
one record at root level, but that isn't correct. This list can have several
items at the root level. So I'm looking for a way to move to next record
within this FindCommentById. I know I could create a foreach loop or
something where FindCommentById is being called but hope istead I could put
it in this method.

private Comment FindCommentById(Guid id)
{
if (this.Id == id)
return this;
else
{
Comment parent = null;
foreach (Comment comment in Replies)
{
parent = comment.FindCommentById(id);
}
return parent;
}
}

any suggestions?
 
F

Family Tree Mike

Jeff said:
From what I understand from reading this text it assume that there is only
one record at root level, but that isn't correct. This list can have
several items at the root level.


What "list" can have several "Comment" objects at the root level?

Your class must be called "Comment", because FindCommentByID returns "this",
and is declared to return Comment, and there is no cast to a Comment object
.. So we can tell your class looks something, minimally, like this:

class Comment
{
private Guid ID { get; set; }
private List<Comment> Replies { get; set; }

private Comment FindCommentById(Guid id)
{
if (this.Id == id) return this;
else
{
Comment parent = null;
foreach (Comment comment in Replies)
parent = comment.FindCommentById(id);
return parent;
}
}
}

It looks like the list of all Comment objects must be stored somewhere else,
right? That would be where you have multiple root objects. So the routine
FindCommentById would only find Comment objects that are this object, or one
of it's replies descendants.

But, hopefully this shows why the question about one root level...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top