Sorry for the stupid question, but I've been there and done that. Just
that just now I have forgotten it for the moment. It happens when
you're in a totally different rut of thought. Bail me out here.
Just to quote an example:
static void Main(string[] args)
{
Console.WriteLine((new Class1()).SomeMethod((Object)19));
}
public int SomeMethod(System.Object obj)
{
if (obj == null)
throw new System.Exception("The argument is null.");
if ((int)obj >= 10)
return (int)obj;
}
Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all
code paths return a value
How do you get out of this?
Kevin Spencer - 26 Jul 2006 15:59 GMT
Hi Walter,
public int SomeMethod(System.Object obj)
{
//...
if ((int)obj >= 10)
return (int)obj;
}
What does it return if (int)obj is NOT greater than 10?

Signature
HTH,
Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist
Sequence, Selection, Iteration.
> Sorry for the stupid question, but I've been there and done that. Just
> that just now I have forgotten it for the moment. It happens when
[quoted text clipped - 20 lines]
>
> How do you get out of this?
Pritcham - 26 Jul 2006 16:01 GMT
Is it because you're not returning a value if the parameter passed is
null? (I know you're throwing an exception but the warning points to
the fact that this particular code path doesn't return anything.
HTH
Martin
> Sorry for the stupid question, but I've been there and done that. Just
> that just now I have forgotten it for the moment. It happens when
[quoted text clipped - 20 lines]
>
> How do you get out of this?
Jon Skeet [C# MVP] - 26 Jul 2006 16:40 GMT
> Is it because you're not returning a value if the parameter passed is
> null? (I know you're throwing an exception but the warning points to
> the fact that this particular code path doesn't return anything.
No, it's not the null code path that's causing the problem - it's the
non-null, less than 10 code path. The compiler is perfectly happy for
something to guarantee it will throw an exception instead of returning
a value.
Jon
Carl Daniel [VC++ MVP] - 26 Jul 2006 16:02 GMT
> Sorry for the stupid question, but I've been there and done that. Just
> that just now I have forgotten it for the moment. It happens when
[quoted text clipped - 14 lines]
> if ((int)obj >= 10)
> return (int)obj;
What if obj is < 10? You still need to return something.
> }
>
> Error: Class1.cs(13): 'Exception.Class1.SomeMethod(object)': not all
> code paths return a value
>
> How do you get out of this?
Return something from all paths, just like the error says.
-cd
Steve Barnett - 26 Jul 2006 16:02 GMT
The question should be... what does SomeMethod return if "obj < 10". You
have defined the function as returning an int, so it must return an int. As
coded, if "obj>=10" then you return a value. If this test fails, you do not
return anything.
Steve
> Sorry for the stupid question, but I've been there and done that. Just
> that just now I have forgotten it for the moment. It happens when
[quoted text clipped - 20 lines]
>
> How do you get out of this?
Mini-Tools Timm - 27 Jul 2006 14:45 GMT
> Sorry for the stupid question, but I've been there and done that. Just
> that just now I have forgotten it for the moment. It happens when
> you're in a totally different rut of thought. Bail me out here.
Just make sure all paths return a value. In your example:
public int SomeMethod(System.Object obj)
{
int i = 0;
if (obj == null)
throw new System.Exception("The argument is null.");
if ((int)obj >= 10)
i = (int)obj;
return i;
}

Signature
Timm Martin
Mini-Tools
.NET Components and Windows Software
http://www.mini-tools.com
> Just to quote an example:
>
[quoted text clipped - 16 lines]
>
> How do you get out of this?