DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Posts
    27

    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?

  2. #2
    Join Date
    Jun 2004
    Posts
    27
    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#.

  3. #3
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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!

  4. #4
    Join Date
    Jun 2004
    Posts
    27
    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.

  5. #5
    Join Date
    Nov 2003
    Location
    Portland, OR
    Posts
    8,387
    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

  1. Use of Select Case or Switch Statement
    By Daryl Shockey in forum Database
    Replies: 2
    Last Post: 05-28-2002, 03:50 PM
  2. wot is CLR
    By lostguy in forum .NET
    Replies: 8
    Last Post: 04-03-2002, 06:41 PM
  3. Brain Washing
    By Danny Bowman in forum .NET
    Replies: 152
    Last Post: 09-13-2001, 07:23 AM
  4. Replies: 0
    Last Post: 04-03-2001, 04:32 PM
  5. switch statement
    By James Lin in forum .NET
    Replies: 9
    Last Post: 08-15-2000, 10:54 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


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


Sponsored Links