468,769 Members | 2,272 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,769 developers. It's quick & easy.

return keyword - invalid error message

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

Nov 17 '05 #1
2 11426
Hi

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

Since 'SouthDade.WorkOrder.WorkOrderPagedIterator.GetNex tPage()' returns
void, a return keyword must not be followed by an object expression
This is using VS2003
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Raj" <ra**********@decadesoftware.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
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

Nov 17 '05 #2
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.

"Raj" <ra**********@decadesoftware.com> wrote in message
news:11********************@g47g2000cwa.googlegrou ps.com...
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

Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Silver | last post: by
6 posts views Thread by Steve Crawford | last post: by
7 posts views Thread by teddarr | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.