Home All Groups Group Topic Archive Search About

How do you get around this?

Author
26 Jul 2006 2:42 PM
Water Cooler v2
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?

Author
26 Jul 2006 2:59 PM
Kevin Spencer
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?

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

Sequence, Selection, Iteration.


Show quote
"Water Cooler v2" <wtr_***@yahoo.com> wrote in message
news:1153924967.384452.239030@m73g2000cwd.googlegroups.com...
> 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?
>
Author
26 Jul 2006 3:01 PM
Pritcham
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
Water Cooler v2 wrote:
Show quote
> 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?
Author
26 Jul 2006 3:40 PM
Jon Skeet [C# MVP]
Pritcham wrote:
> 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
Author
26 Jul 2006 3:02 PM
Carl Daniel [VC++ MVP]
Water Cooler v2 wrote:
Show quote
> 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;

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
Author
26 Jul 2006 3:02 PM
Steve Barnett
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


Show quote
"Water Cooler v2" <wtr_***@yahoo.com> wrote in message
news:1153924967.384452.239030@m73g2000cwd.googlegroups.com...
> 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?
>
Author
27 Jul 2006 1:45 PM
Mini-Tools Timm
"Water Cooler v2" wrote:

> 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;
}

--
Timm Martin
Mini-Tools
..NET Components and Windows Software
http://www.mini-tools.com


Show quote
>
> 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?
>
>

AddThis Social Bookmark Button