【发布时间】:2016-09-28 15:52:21
【问题描述】:
如何在 switch case 中使用枚举值? 这是我的代码:
//public Variables
double rat;
string M;
public enum operations {add = 1, sub = 2, mult = 3, div = 4} ;
bool NewText = false;
private void btnmult_Click(object sender, EventArgs e)
{
//Button "*"
operations value = operations.mult;
rat = Convert.ToDouble(result.Text);
label1.Text = rat + " *";
result.Text = "";
}
private void EqualButton_Click(object sender, EventArgs e)
{
switch (operations) // this is the point which is am confused at
{
case (operations.mult): //multiplyication
TheEqualMult(rat);
label1.Text = "";
break;
}
}
现在我应该在 () 之间切换单词之后写什么?我要输入的是枚举值,它指的是 1 或 2 或 3 或 4,以便按钮可以知道哪个是正确的操作。
【问题讨论】:
-
switch (value),如果value包含您感兴趣的值。顺便说一句,请考虑关注.NET naming conventions。诸如类、枚举、方法之类的东西在 .NET 中是 PascalCased。 -
我遇到了案例 (operations.mult) "Windows_Calculator.Form1.operations' 到 'int'。存在显式转换(您是否缺少演员表?)"
-
附注:根据经验,枚举应命名为单数(即
operation),除非它们可以组合。
标签: c# enums switch-statement calculator