I am trying to convert a JavaScript switch to a C# one.
I am getting error Error 3 Cannot implicitly convert type 'bool' to 'int'
switch (numero)
{
case (numero > -1 && numero < 16):
rtn = list1[numero] + " " + str2;
if (primerDigito != 1)
{
rtn += "s";
}
break;
case (numero > 15 && numero < 30):
if (numero != 20)
{
rtn = list2[primerDigito] + list1[segundoDigito];
}
else
{
rtn = "veinte";
}
rtn += " " + str2 + "s";
break;
case (numero > 29 && numero < 100):
rtn = list2[primerDigito] + "nta";
if (segundoDigito != 0)
{
rtn += " y " + list1[segundoDigito];
}
rtn += " " + str2 + "s";
break;
case 100:
rtn = "cien " + str2 + "s";
break;
default:
rtn = "número invalido";
}
Is there a work around for this?
Note: I really MUST use switch-case (and not if-elses)
You cannot do this: a
switchstatement can only use single values, not ranges for thecases.You need an
if/elsetree instead. Something in the form of:Note that if you’re absolutely, totally committed to using a
switch, you would have to enumerate all of the cases (but this is such an egregious sin against software engineering that I can’t even believe I’m writing it out – do not do this, it’s just an example of how switch statements work):If I saw this in my codebase, though, I would be baffled and angry.