【发布时间】:2014-07-02 14:20:52
【问题描述】:
我的 IF Else 结构
if (e.Exception.Message.Contains("'TierType' element is invalid"))
{
objError.ErrorCode = "EC005";
objError.ErrorMessage = "TierType is incorrect"; //constant file
}
else if (e.Exception.Message.Contains("'Gender' element is invalid"))
{
objError.ErrorCode = "EC010";
objError.ErrorMessage = "Gender is incorrect"; //constant file
}
else
{
objError.ErrorCode = "EC050";
objError.ErrorMessage = e.Exception.Message;//constant file
}
我需要改变这个结构来切换大小写
e.Exception.Message.Contains(); //this contain 10 different values
我尝试跟随它的显示错误
string Exceptionmessage = e.Exception.Message;
switch (Exceptionmessage)
{
case Exceptionmessage.Contains("'TierType' element is invalid"):
//case "'TierType' element is invalid":
objError.ErrorCode = "EC005";
objError.ErrorMessage = "TierType is incorrect";
break;
default:
objError.ErrorCode ="EC050";
objError.ErrorMessage = e.Exception.Message;
break;
}
我知道我尝试了错误的方法请告诉我如何为我的要求编写开关结构。
【问题讨论】:
-
If/Else 有什么问题?据我所知,switch case 标签用于相等性检查。
-
您不能在开关中使用包含
-
为什么“需要”将其转换为
switch? -
我想您可以先对异常消息进行处理,例如将“TierType”和“Gender”提取到一个字符串中,然后打开它。但是,您仍然应该使用 If/Else。
-
switch只能用于将单个变量与 const 值的集合/范围进行比较。
标签: c# if-statement switch-statement