从switch语句返回值到方法 [英] Returning values to a method from a switch statement
问题描述
我需要在方法中嵌套
的switch语句中返回一个方法调用的值。怎么办呢?
int myMethod(int someValue)
{
switch(someValue)
{
case(1):
{
返回anotherMethod()
}
case(2):
{
return somethingElse()
}
}
}
当我这样做时,我收到编译错误,说并非所有代码路径都返回
值。我理解为什么我得到这个,因为我没有给
主要方法返回一个值。我的问题是,如何从交换机返回值?
提前谢谢
Ant
Hi, I need to return a value to a methods call from a switch statement nested
in the method. How can this be done?
int myMethod(int someValue)
{
switch (someValue)
{
case(1):
{
return anotherMethod()
}
case(2):
{
return somethingElse()
}
}
}
When I do this, I get a compile error saying, "not all code paths return a
value". I understand why I''m getting this as I''m not returning a value to the
main method. My question is, how do I return the value from the switch?
Thanks in advance
Ant
推荐答案
将一个return语句放在一个开关中没有错。
你得到这个错误的原因是因为该方法不会返回
如果someValue不是1或2的话 - 有一个可能的代码路径
直接导致方法结束,跳过两个返回
报表。 (即使你知道someValue总是1或2,
编译器也不知道。)
你可以通过返回一个来修复它切换后默认值或抛出异常
,或者在交换机上添加默认情况并执行它
那里:
开关(someValue)
{
案例1:
返回anotherMethod();
案例2:
返回somethingElse();
默认值:
抛出新的ArgumentException(someValue必须为1或2);
}
Jesse
There''s nothing wrong with putting a return statement inside a switch.
The reason you''re getting that error is because the method won''t return
anything if someValue isn''t 1 or 2 - there''s a possible code path that
leads straight to the end of the method, skipping both return
statements. (Even if you know someValue will always be 1 or 2, the
compiler doesn''t know that.)
You can fix it by returning a default value or throwing an exception
after the switch, or adding a default case to the switch and doing it
there:
switch (someValue)
{
case 1:
return anotherMethod();
case 2:
return somethingElse();
default:
throw new ArgumentException("someValue must be 1 or 2");
}
Jesse
您还需要一个默认:声明,因为有更多可能的价值比某些价值超过1和2(如果有人打电话给
myMethod(42)会怎样?)。此外,每个案例陈述后的花括号是
superflous,以及1和2左右的parens。
玩得开心。
You also need a "default:" statement, since there are more possible
values to someValue than 1 and 2 (what happens if someone calls
myMethod(42)?). Also, the curly braces after each case statement are
superflous, as are the parens around 1 and 2.
Have fun.
嗨Ant,
作为最佳实践,您应该尝试使用单点退出编程
在你的程序中,你永远不会遇到像你这样的问题
现在面临的问题。
所以这样的事情。
int myMethod(int someValue)
{
int result;
switch(someValue)
{
case(1):
{
result = 12;
}
case (2):
{
result = 13;
}
}
返回结果;
}
亲切的问候,
-
Rainier van Slingerlandt
(自由培训师/顾问/开发人员)
www.slingerlandt.com
&qu OT;蚂蚁"写道:
Hi Ant,
As a best practice you should try programming using a single point of exit
within your procedures that way you will never walk into problems like you
are facing right now.
So something like this.
int myMethod(int someValue)
{
int result;
switch (someValue)
{
case(1):
{
result = 12;
}
case(2):
{
result = 13;
}
}
return result;
}
Kind Regards,
--
Rainier van Slingerlandt
(Freelance trainer/consultant/developer)
www.slingerlandt.com
"Ant" wrote:
我需要在方法中嵌套
的switch语句中返回一个方法调用的值。怎么办呢?
int myMethod(int someValue)
{
switch(someValue)
{
case(1) :
{
返回anotherMethod()
}
案例(2):
{
返回一些东西()
}
}
当我这样做时,我收到编译错误,说并非所有代码路径都返回
值。我理解为什么我得到这个,因为我没有给
主要方法返回一个值。我的问题是,如何从交换机返回值?
提前致谢
Ant
Hi, I need to return a value to a methods call from a switch statement nested
in the method. How can this be done?
int myMethod(int someValue)
{
switch (someValue)
{
case(1):
{
return anotherMethod()
}
case(2):
{
return somethingElse()
}
}
}
When I do this, I get a compile error saying, "not all code paths return a
value". I understand why I''m getting this as I''m not returning a value to the
main method. My question is, how do I return the value from the switch?
Thanks in advance
Ant
这篇关于从switch语句返回值到方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!