如何在case语句中使用变量? [英] How to use variable in case statement?

查看:331
本文介绍了如何在case语句中使用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想使用:


开关(AppName)

{

case ApplicationName.App1:

loadApp1Logo();

break;

case ApplicationName.App2:

loadApp2Logo();

break;

}


我会收到编译错误:

预计会有一个常数值


这意味着我不能使用上面的枚举。我不想在case语句中硬编码

AppName。 AppName从上面提到的

得到它的值。


我可以使用If / elseif语句轻松解决这个问题但案例

语句是更清晰的代码,因为我有一些条件。有没有

a的方式用case语句或其他更好的方式呢?


谢谢,

Brett

If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I''ll get a compiler error:
A constant value is expected

Which means I can''t use the above enums. I don''t want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I''ll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett

推荐答案

枚举不能定义为字符串,所以你能否澄清你的陈述...

上面提到的相同的精确词汇?


Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...
Enums cannot be defined as strings, so can you clarify your statement "...
value from the same exact enums presented above"?

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
如果我想使用:

开关(AppName)
案例ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}
我将收到编译器错误:
一个常量价值是预期的

这意味着我不能使用上面的枚举。我不想在case语句中硬编码
AppName。 AppName从上面提到的相同的枚举中获得它的价值。

我可以使用If / elseif语句轻松解决这个问题,但是案例
语句是更清晰的代码,因为我'会有一些条件。是否有一种方法可以通过案例陈述或其他更好的方式来做到这一点?

谢谢,
Brett
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I''ll get a compiler error:
A constant value is expected

Which means I can''t use the above enums. I don''t want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I''ll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett


Brett,


你确定它在AppName上,而不在ApplicationName.App1上(或者

App2)。这些必须是枚举值或常量。它们不能是
变量(它们是属性,还是暴露的字段?)。


此错误适用于case语句,不适用于AppName。 AppName可以

是一个常数(但这是没有意义的)或变量。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- mv*@spam.guard.caspershouse.com


" Brett Romero" <交流***** @ cygen.com>在消息中写道

news:11 ********************* @ g47g2000cwa.googlegro ups.com ...
Brett,

Are you sure it is on AppName, and not on ApplicationName.App1 (or
App2). These need to be enumeration values, or constants. They can''t be
variables (are they properties, or fields that are exposed?).

This error is for the case statements, not for the AppName. AppName can
be a constant (but that would be pointless), or a variable.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Brett Romero" <ac*****@cygen.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
如果我想使用:

开关(AppName)
案例ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}
我将收到编译器错误:
一个常量价值是预期的

这意味着我不能使用上面的枚举。我不想在case语句中硬编码
AppName。 AppName从上面提到的相同的枚举中获得它的价值。

我可以使用If / elseif语句轻松解决这个问题,但是案例
语句是更清晰的代码,因为我'会有一些条件。是否有一种方法可以通过案例陈述或其他更好的方式来做到这一点?

谢谢,
Brett
If I want to use:

switch (AppName)
{
case ApplicationName.App1:
loadApp1Logo();
break;
case ApplicationName.App2:
loadApp2Logo();
break;
}

I''ll get a compiler error:
A constant value is expected

Which means I can''t use the above enums. I don''t want to hardcode the
AppName in the case statements. AppName is getting its value from the
same exact enums presented above.

I can easily get around this with If/elseif statements but the case
statements are cleaner code since I''ll have a few conditions. Is there
a way to do this with case statements or some other better way?

Thanks,
Brett


对不起,我不清楚。这样,我不想在案例陈述中硬编码AppName

。我的意思是说ApplicationName.x。错误

在枚举上,而不是AppName。


这是其中一个枚举:


public struct ApplicationName

{

public static string App1 =" MyApp1" ;;


所以它实际上是你建议的枚举尼古拉斯。但是我得错了

.


谢谢,

Brett

Sorry, I wasn''t clear. By this, "I don''t want to hardcode the AppName
in the case statements". I meant to say ApplicationName.x. The error
is on the enums and not AppName.

Here is one of the enums:

public struct ApplicationName
{
public static string App1 = "MyApp1";

So it is actually an enumeration as you suggested Nicholas. But I get
the error.

Thanks,
Brett


这篇关于如何在case语句中使用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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