-
C# switch statement - Unreachable code detected -
I can't for the life of me figure out why I keep getting this error. This is the code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MyFunctions
{
public class TextFunctions
{
public string GetMonth(int num)
{
try
{
switch (num)
{
case 1:
return "January";
break;
case 2:
return "February";
break;
case 3:
return "March";
break;
case 4:
return "April";
break;
case 5:
return "May";
break;
case 6:
return "June";
break;
case 7:
return "July";
break;
case 8:
return "August";
break;
case 9:
return "September";
break;
case 10:
return "October";
break;
case 11:
return "November";
break;
case 12:
return "December";
break;
default:
MessageBox.Show("No such month!! Month number must be 1 to 12.");
return "Number out of range";
break;
}
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
}
}
The compiler flags the 'break' statements with this message. It also flags the GetMonth label with "not all code paths return a value". What am I doing wrong?
-
I came upon this link http://support.microsoft.com/kb/120967 which indicates that this problem is a bug in C++. Apparently, it has been carried over into C#.
-
You can avoid the errors by rewriting the code like this:
Code:
static string GetMonth(int num)
{
string returnValue = "";
try
{
switch (num)
{
case 1:
returnValue = "January";
break;
case 2:
returnValue = "February";
break;
case 3:
returnValue = "March";
break;
case 4:
returnValue = "April";
break;
case 5:
returnValue = "May";
break;
case 6:
returnValue = "June";
break;
case 7:
returnValue = "July";
break;
case 8:
returnValue = "August";
break;
case 9:
returnValue = "September";
break;
case 10:
returnValue = "October";
break;
case 11:
returnValue = "November";
break;
case 12:
returnValue = "December";
break;
default:
returnValue = "Number out of range";
break;
}
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message);
}
return returnValue;
}
Or better still, do this:
return new System.Globalization.DateTimeFormatInfo().GetMonthName(num);
;-)
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks Phil. I just knew there had to be a easier way than mine. I just hadn't gotten to the chapter in my book that deals with date and time. As usual, you've provided an eloquent solution to the problem. Thanks again.
-
FYI, I found it by going into Visual Studio's Object Browser and searching for "MonthName".
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
Similar Threads
-
By Daryl Shockey in forum Database
Replies: 2
Last Post: 05-28-2002, 03:50 PM
-
Replies: 8
Last Post: 04-03-2002, 06:41 PM
-
By Danny Bowman in forum .NET
Replies: 152
Last Post: 09-13-2001, 07:23 AM
-
By michael s in forum XML
Replies: 0
Last Post: 04-03-2001, 04:32 PM
-
By James Lin in forum .NET
Replies: 9
Last Post: 08-15-2000, 10:54 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks