LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C# Control cannot fall through from one case label to another? (https://www.linuxquestions.org/questions/programming-9/c-control-cannot-fall-through-from-one-case-label-to-another-865373/)

michaelinux 02-27-2011 08:33 PM

C# Control cannot fall through from one case label to another?
 
I'm having this problem with this piece of code, and i don't really get what the problem is, maybe is because i am already too sleepy to concentrate enough but maybe you guys can help me:


switch(plazo){
case 1:
plazo = 12;
break;
case 2:
plazo = 24;
break;
case 3:
plazo = 36;
break;
default:
Console.WriteLine("Ingrese un plazo valido de 1 a 3");
}


sorry if you don't understand the messages printing, it's Spanish.
anyway the error says Control cannot fall through from one case label to another
and it highlights the switch statement, i had this switch within a do-while loop, and i thought that might be the problem. but it wasn't
:(
i hope you can help me.
many thanks :)

corp769 02-27-2011 08:42 PM

You are getting that error because in C#, every case needs to end with either goto, break, or return. The proper way for your example would be the following:

Code:

switch(plazo){
case 1:
plazo = 12;
break;
case 2:
plazo = 24;
break;
case 3:
plazo = 36;
break;
default:
Console.WriteLine("Ingrese un plazo valido de 1 a 3");
break;
}


michaelinux 02-27-2011 09:42 PM

Quote:

Originally Posted by corp769 (Post 4273424)
You are getting that error because in C#, every case needs to end with either goto, break, or return. The proper way for your example would be the following:

Code:

switch(plazo){
case 1:
plazo = 12;
break;
case 2:
plazo = 24;
break;
case 3:
plazo = 36;
break;
default:
Console.WriteLine("Ingrese un plazo valido de 1 a 3");
break;
}


Thank you very much that break solved it.

corp769 02-27-2011 10:19 PM

Not a problem.

Cheers,

Josh


All times are GMT -5. The time now is 09:29 PM.