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 / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

strange compilation warning

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Andy Fish - 04 Sep 2007 12:30 GMT
The following code:

enum Foo { one, two };
int Bar(Foo foo)
{
  switch (foo)
  {
     case Foo.one:
        return 7;
     case Foo.two:
        return 4;
  }
}

gives a compilation warning "not all paths return a value"

surely this is spurious though? - there is no path I can see that does not
return a value

Andy
Marc Gravell - 04 Sep 2007 12:41 GMT
> there is no path I can see that does not return a value
How about:
Foo foo = (Foo) 17; // this works just fine
Bar(foo); //boom

Best option: add a
default: throw new ArgumentException();
line...

Marc
Andy Fish - 04 Sep 2007 12:43 GMT
>> there is no path I can see that does not return a value
> How about:
[quoted text clipped - 6 lines]
>
> Marc

Ooh - I didn't know you could cast an int to an enum even if it was out of
range

Thanks Marc
Arne Vajhøj - 10 Sep 2007 01:53 GMT
>>> there is no path I can see that does not return a value
>> How about:
[quoted text clipped - 7 lines]
> Ooh - I didn't know you could cast an int to an enum even if it was out of
> range

C# enum is almost just an int.

If I were to guess at why, then I would say that it is necesarry
to support using multiple values in one variable (flags).

Arne

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.