分配变量枚举类型,具体取决于另一个变量的值 [英] assigning variable enum type, depending on value of another variable
问题描述
请原谅我的问题,我打赌这是一个简单的(和愚蠢的)!
我有一个变量(日),我希望将值设置为两个不同的枚举,具体取决于另一个变量的状态(WeekPeriod)。它是这样的:
enum工作日
{
星期一,
星期二,
星期三,
星期四,
星期五
}
enum周末
{
周六,
周日
}
...
if(WeekPeriod ==Weekend)
{
工作日;
}
其他
{
周末日;
}
我希望这有道理 - 你有什么建议吗?!!
提前谢谢
Mike
Excuse my question, I bet this is an easy (and dumb) one!
I have a variable (Day), which I wish to set the value to two different enums, depending on the state of another variable (WeekPeriod). Its something like this:
enum Weekday
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday
}
enum Weekend
{
Saturday,
Sunday
}
...
if(WeekPeriod == "Weekend")
{
Weekday Day;
}
else
{
Weekend Day;
}
I hope this makes sense - do you have any suggestions?!!
Thanks in advance
Mike
推荐答案
可以声明整数变量然后使用强制转换操作
it is possible to declare integer variable and then use cast operation
int Day;
if(WeekPeriod == "Weekend")
{
Day = (int)someValue1; // someValue1 is Weekend
}
else
{
Day = (int)someValue2; // someValue2 is Weekday
}
这篇关于分配变量枚举类型,具体取决于另一个变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!