【问题标题】:How to convert this if else structure to switch case?如何将此 if else 结构转换为 switch case?
【发布时间】: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


【解决方案1】:

正如在不同的 cmets 中回答的那样,switch 语句需要编译时常量,但如果您想要另一种方式,您可以创建一个类,在其中初始化您将管理的不同异常的列表。

在这个类中添加一个方法来处理异常或管理默认异常。

这是一种方法:

class Program
{
    public static void Main(string[] args)
    {
        try
        {
            throw new Exception("'Gender' element is invalid");
        }
        catch (Exception ex)
        {
            Error err = Error.ReadError(ex);
        }
    }
}

public class Error
{
    public string ErrorCode { get; set; }
    public string ErrorMessage { get; set; }
    public string ErrorRaw { get; set; }
    public static List<Error> ErrorList { get; private set; }

    static Error()
    {
        ErrorList = new List<Error>()
        {
            new Error(){
                ErrorCode = "EC005",
                ErrorMessage = "TierType is incorrect",  
                ErrorRaw = "'TierType' element is invalid"
            },
            new Error(){
                ErrorCode = "EC010",
                ErrorMessage = "Gender is incorrect",  
                ErrorRaw = "'Gender' element is invalid"
            },

        };
    }

    public static Error DefaultError(Exception ex)
    {
        return new Error(){
                ErrorCode = "EC050",
                ErrorMessage = ex.Message
            };
    }

    public static Error ReadError(Exception ex)
    {
        Error error = ErrorList.FirstOrDefault(e => ex.Message.Contains(e.ErrorRaw));
        if (error == null)
        {
            error = Error.DefaultError(ex);
        }

        return error;
    }
}

【讨论】:

    猜你喜欢
    • 2021-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-03
    • 2019-07-04
    • 2014-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多