Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2006

Tip: Looking for answers? Try searching our database.

How do you get around this?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Water Cooler v2 - 26 Jul 2006 15:42 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 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?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.