return keyword - invalid error message

  • Thread starter Thread starter Raj
  • Start date Start date
R

Raj

Hi,

I was getting an error in my program saying that return keyword must
not be followed by an object expression. I was confused first because I
was using return statement in a valid "if" statement.
e.g. if(expression) return false;

Later, I found that this error was due to a mistake in my method
declaration. I had put void as a return type in my method. When I
changed this to bool, everything works.

I would expect a more meaningfule error message for this type of
errors.

I'm posting this here just to let others know..

Thanks,
Raj
 
Hi

I think you read it wrong, this is the message I get:

Since 'SouthDade.WorkOrder.WorkOrderPagedIterator.GetNextPage()' returns
void, a return keyword must not be followed by an object expression


This is using VS2003


cheers,
 
Raj,

Actually, the error message makes perfect sense. Keep in mind that the
return keyword is technically part of every method, regardless of type.
However, for methods of type void it *may* be omitted. Thus, the following
code would be perfectly valid:

public void Foo()
{
// do some stuff

return;
}

However, in case of a method of type void, you must not include an
expression after the return keyword since the method does not return any
data, so the following won't compile:

public void Foo()
{
// do stuff

return 0;
}

Also, if I'm not mistaken, the actual error message should look something
like this:

Since 'Namespace.Class.Method' returns void, a return keyword must not be
followed by an object expression

So the message indicates that the object expression is invalid because the
method type is void.
--
Kai Brinkmann [MSFT]

Please do not send e-mail directly to this alias. This alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top