【发布时间】:2012-09-30 22:05:11
【问题描述】:
为什么会出现以下编译错误:
//错误CS0159:没有这样的标签'lbl_proc_20'
使用以下代码:
//JUST A DUMMY CODE TO ILLUSTRATE THE CONCEPT
int a = resultOfFunction1();
int b = resultOfFunction2();
//10+ Local variables that are calculated depending on the results above
if (a < 10)
{
switch (b)
{
case 0:
//Actions for A<10, B=0, using local variables
break;
case 1:
double c = someFunction(a, b); //In real code involves calculations based on a and b
if(c > 10.0)
goto lbl_proc_20; //error CS0159: No such label 'lbl_proc_20' within the scope of the goto statement
//Actions for A<10, B=1, using local variables
break;
default:
//Actions for A<10, B=Other, using local variables
break;
}
}
else if (a < 20)
{
lbl_proc_20:
switch(b)
{
case 0:
//Actions for A<20, B=0, using local variables
break;
case 1:
//Actions for A<20, B=1, using local variables
break;
case 2:
//Actions for A<20, B=2, using local variables
break;
default:
//Actions for A<20, B=Other, using local variables
break;
}
}
else if (a < 30)
{
switch(b)
{
case 0:
//Actions for A<30, B=0, using local variables
break;
case 1:
//Actions for A<30, B=1, using local variables
break;
case 2:
//Actions for A<30, B=2, using local variables
break;
default:
//Actions for A<30, B=Other, using local variables
break;
}
}
为什么我会收到 goto 语句错误以及如何使其工作?
编辑:更改了示例以说明实际代码。
【问题讨论】:
-
'goto' 一定是编程界的新名词,哈哈 :D
-
@user843732 - 是的,超过 30 年。有充分的理由,而不仅仅是微软。
-
@user843732 我建议您重新编写代码。只有非常非常非常特殊的情况可以选择 goto。