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
Unreachable code is caused by the return statement in default block of code:
The solution is to simply bring return statement to the end of your default block:
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
:Or you can even simplify your code like this:
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.
If you need that statement to do something it’s probably in the wrong place!
What I think you meant was this.
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)…