我正在检查文本框控件“txtType”中的一个值,我有 3 个可能的值:它是 TypeA、TypeB 或 TypeC。所以我想做的就是: 这是我目前所拥有的
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updatedType;
If string myType = ‘TypeA’ then set updatedType to 1
If string myType = ‘TypeB’ then set updatedType to 2
If string myType = ‘TypeC’ then set updatedType to 3
我曾尝试使用 switch 语句但搞砸了。
最佳答案
试试这个:
string myType= ((TextBox)DV_LogAdd.FindControl("txtType")).Text.ToString();
int updateType = 0;
switch (myType)
{
case "TypeA":
updateType = 1;
break;
case "TypeB":
updateType = 2;
break;
case "TypeC":
updateType = 3;
break;
default :
throw new ArgumentException("txtType value not supported :"+myType);
}
关于c# - 尝试在 C# 中使用 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16966393/