来自Switch的调用方法{} [英] Calling Method From Switch{}

查看:64
本文介绍了来自Switch的调用方法{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Switch调用另一个方法可以吗?


例如。


Switch(stringVar)

{

case(" a"):

somVar =" whatever";

Another_Method(); //调用另一种方法

return;

case(" g"):

somVar =" somethingelse";

Another_Method_2(); //调用另一种方法

返回;

}


这是推荐的方法还是应该只是if语句?

Is it fine to call another method from Switch?

Eg.

Switch (stringVar)
{
case ("a"):
somVar = "whatever";
Another_Method(); //call another method
return;
case ("g"):
somVar = "somethingelse";
Another_Method_2(); //call another method
return;
}

Is this a recommended approach or should it just be an if statement?

推荐答案

msnews.microsoft.com写道:
msnews.microsoft.com wrote:

是吗可以从Switch调用另一种方法吗?


例如。


Switch(stringVar)

{

case(" a"):

somVar =" whatever";

Another_Method(); //调用另一种方法

return;

case(" g"):

somVar =" somethingelse";

Another_Method_2(); //调用另一种方法

返回;

}


这是推荐的方法还是应该只是if语句?
Is it fine to call another method from Switch?

Eg.

Switch (stringVar)
{
case ("a"):
somVar = "whatever";
Another_Method(); //call another method
return;
case ("g"):
somVar = "somethingelse";
Another_Method_2(); //call another method
return;
}

Is this a recommended approach or should it just be an if statement?



没关系。就个人而言,无论何时

替代品依赖于相同的源数据,我都会发现最好使用switch语句。您可以将以上内容写成:


if(stringVar =" a")

{

// ...

}

else if(stringVar =" g")

{

// ... <
}


....但我不会。使用开关

比使用堆栈if语句测试相同变量更清晰。


-cd

It''s fine. Personally I find a switch statement preferable any time the
alternatives depend on the same source data . You could write the above as:

if (stringVar="a")
{
// ...
}
else if (stringVar="g")
{
// ...
}

....but I wouldn''t. The structure of the code is clearer using the switch
than using stacked if statements testing the same variable.

-cd


Carl Daniel [VC ++ MVP]写道:
Carl Daniel [VC++ MVP] wrote:

没关系。就个人而言,无论何时

替代品依赖于相同的源数据,我都会发现最好使用switch语句。您可以将以上内容写成:


if(stringVar =" a")

{

// ...

}

else if(stringVar =" g")

{

// ... < br $> b $ b}


...但我不会。使用开关

比使用堆栈if语句测试相同变量更清晰。
It''s fine. Personally I find a switch statement preferable any time the
alternatives depend on the same source data . You could write the above as:

if (stringVar="a")
{
// ...
}
else if (stringVar="g")
{
// ...
}

...but I wouldn''t. The structure of the code is clearer using the switch
than using stacked if statements testing the same variable.



==


Arne

==

Arne


虽然不是语法或在语义上不正确,明确使用开关来调用方法不是面向对象的。您应该考虑使用继承

和多态。


-

浏览 http://connect.microsoft.com/VisualStudio/feedback/ 并投票。
http://www.peterRitchie.com/blog/

Microsoft MVP,Visual Developer - Visual C#

" msnews.microsoft.com"写道:
While not syntactically or semantically incorrect, clearly use of a switch to
invoke methods isn''t object-oriented. You should consider using inheritance
and polymorphism instead.

--
Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.
http://www.peterRitchie.com/blog/
Microsoft MVP, Visual Developer - Visual C#
"msnews.microsoft.com" wrote:

从Switch调用另一种方法可以吗?


例如。


Switch(stringVar)

{

case(" a"):

somVar =" whatever";

Another_Method(); //调用另一种方法

return;

case(" g"):

somVar =" somethingelse";

Another_Method_2(); //调用另一种方法

返回;

}


这是推荐的方法还是应该只是if语句?

Is it fine to call another method from Switch?

Eg.

Switch (stringVar)
{
case ("a"):
somVar = "whatever";
Another_Method(); //call another method
return;
case ("g"):
somVar = "somethingelse";
Another_Method_2(); //call another method
return;
}

Is this a recommended approach or should it just be an if statement?


这篇关于来自Switch的调用方法{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆