skip to Main Content
public string CheckStatus(int status)
{
    string Ticket;

    switch (status) 
    {
        case 1:
            Ticket = "Open";
            return Ticket;

        case 2:
            Ticket = "Pending";
            return Ticket;

        case 3:
            Ticket = "In Progress";
            return Ticket;

        case 4:
            Ticket = "Escalated";
            return Ticket;

        case 5:
            Ticket = "Closed";
            return Ticket;

        default:
            Ticket = "Null";
            return Ticket;

            lblStatus.Text = ("Status: " + Ticket);
    }
}

Warning CS0162 Unreachable code detected
AS-Ticketing-Systems C:Userss0300060DownloadsAS-Ticketing-SystemsAS-Ticketing-SystemswebpagesitsupportSelectedItTicket.aspx.cs 141 Active

I want it to change the label to the string tickets

2

Answers


  1. Unreachable code is caused by the return statement in default block of code:

    default:
        Ticket = "Null";
        return Ticket; //there it is. Even compiler should give you the warning here
    
        lblStatus.Text = ("Status: " + Ticket);
    

    The solution is to simply bring return statement to the end of your default block:

    default:
        Ticket = "Null";
        
        lblStatus.Text = ("Status: " + Ticket);
        return Ticket;
    

    Also the good tip is to use break keywork in each case and default blocks and simply in the end of the method write return Ticket:

    public string CheckStatus(int status)
    {
    
        string Ticket;
        switch (status) 
        {
            case 1:
                Ticket = "Open";
                break;
            case 2:
                Ticket = "Pending";
                break;
            case 3:
                Ticket = "In Progress";
                break;
            case 4:
                Ticket = "Escalated";
                break;
            case 5:
                Ticket = "Closed";
                break;
            default:
                Ticket = "Null";
                lblStatus.Text = ("Status: " + Ticket);
                break;
    
        }
        return Ticket;
    }
    

    Or you can even simplify your code like this:

    public string CheckStatus(int status)
    {
        switch (status) 
        {
            case 1: return "Open";
            case 2: return "Pending";
            case 3: return "In Progress";
            case 4: return "Escalated";
            case 5: return "Closed";
            default:
                lblStatus.Text = "Status: Null";
                return "Null";
    
        }
    }
    
    Login or Signup to reply.
  2. The warning (not error) is telling you something doesn’t look right. Taking a look at the code the line below is unreachable and may as well be removed.

                lblStatus.Text = ("Status: " + Ticket);
    

    If you need that statement to do something it’s probably in the wrong place!

    What I think you meant was this.

    public string CheckStatus(int status)
    {
        string Ticket;
    
        switch (status) 
        {
            case 1:
                Ticket = "Open";
                break;
    
            case 2:
                Ticket = "Pending";
                break;
    
            case 3:
                Ticket = "In Progress";
                break;
    
            case 4:
                Ticket = "Escalated";
                break;
    
            case 5:
                Ticket = "Closed";
    
            default:
                Ticket = "Null";
        }
    
        lblStatus.Text = ("Status: " + Ticket);
        return Ticket;
    }
    

    Switch Expression

    .NET introduced switch expressions that would simplify this even further but you’ve tagged [asp.net] rather than [asp.net-core]. Just in case though (and with a little tidying up)…

    public string CheckStatus(int status)
    {
        var ticket = status switch
        {
            1 => "Open",
            2 => "Pending",
            3 => "In Progress",
            4 => "Escalated",
            5 => "Closed",
            _ => "Null",
        }
    
        lblStatus.Text = $"Status: {ticket}";
        return ticket;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search