not all code paths return a value

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

Jeff

Hi

..NET 2.0

I get this compile error:
"not all code paths return a value"

What code path is it talking about here?

Here is my code:

private Comment FindCommentById(Guid id)
{
if (this.Id == id)
return this;
else
{
foreach (Comment comment in Replies)
{
Comment parent = comment.FindCommentById(id);
if (parent != null)
return parent;
else
return null;
}
}
}
 
Jeff said:
I get this compile error:
"not all code paths return a value"

What code path is it talking about here?

Here is my code:

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

If there is nothing in Replies then you do not get
into the foreach loop and then you are indeed
missing a return value.

You may know that Replies are never empty, but the
compiler does not.

Arne
 
Jeff said:
Hi

.NET 2.0

I get this compile error:
"not all code paths return a value"

What code path is it talking about here?

Here is my code:

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

The code path where there are no comments in the Replies enumeration, in
which case none of the content of the for each will execute so the end of
the function will be reached without encountering a return statement.
Without other refactors your code ought to look like this:-

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

Ok I couldn't bring myself not refactor a little more. The inner if was
pointless.

Still its a bizarre chunk of code. You are enumerating but terminating the
loop after only one iteration. Does Replies not have an ordinal indexer?
 
- You are enumerating but terminating the loop after only one iteration.
I guesss I am but that is a bug. I see now that issue. so I'm trying to
implement that code example you gave me without using "break;"

Not sure what you mean with "Does Replies not have an ordinal indexer"

This is a comment system for a webportal I'm trying to construct. So some
comments can be a reply to another comment. And one comment can have may
replies... So Replies are a collection of Comments
 
Jeff said:
- You are enumerating but terminating the loop after only one iteration.
I guesss I am but that is a bug. I see now that issue. so I'm trying to
implement that code example you gave me without using "break;"

Not sure what you mean with "Does Replies not have an ordinal indexer"

What I mean is the ability access an item in the collection via is ordinal
offset. The first comment in the collection could be accessed using:-

Comment x = Replies[0];
 
Jeff said:
Hi

.NET 2.0

I get this compile error:
"not all code paths return a value"

What code path is it talking about here?

Here is my code:

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

I believe that you want to loop through all the replies before returning
null. Otherwise you will only find a comment if it's the first reply.

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