大写字母和 if else laddder

本文关键字:laddder else if 大写字母 | 更新日期: 2023-09-27 18:36:24

我是.NET的新手,在我写的下面的代码中,首先需要关键字将其转换为大写,然后如果其他阶梯。只需检查正确性,这是我的代码,

 private string toupper(string keyword)
    {
        newkeyword = keyword.ToUpper();
        return newkeyword;
    }
    private string check(String newkeyword)
    {
        if (newkeyword == SETTELG || SETTHJORT)
        {
            Response.Redirect("../SMSFunction/SeenSMS.ascx");
        }
        else if (newkeyword==SKUTTELG || SKUTTHJORT)
        {
            Response.Redirect("../SMSFunction/ShotSMS.ascx");
        }
        else if (newkeyword == RUNDBALL)
        {
            Response.Redirect("../SMSFunction/RoundballSMS.ascx");
        }
    }

大写字母和 if else laddder

StringDictionary

区分大小写,因此您可以避免使用上限/下限 - 因此作为字段

readonly StringDictionary redirects = new StringDictionary {
    {SETTELG,    "../SMSFunction/SeenSMS.ascx"},
    {SETTHJORT,  "../SMSFunction/SeenSMS.ascx"},
    {SKUTTELG,   "../SMSFunction/ShotSMS.ascx"},
    {SKUTTHJORT, "../SMSFunction/ShotSMS.ascx"},
    {RUNDBALL,   "../SMSFunction/RoundballSMS.ascx"},
};

然后只是:

var path = redirects[keyword];
if(path != null) Response.Redirect(path);
private void Check(string keyword)
    {
        switch(keyword.ToUpper())
        {
            case "SETTELG ":
            case  "SETTHJORT":
                Response.Redirect("../SMSFunction/SeenSMS.ascx");
                break;
                /*remaining code*/
        }
    }

制作如下...

当涉及到关键字和其他(对于域)众所周知的字符串值时,我更喜欢使用某种解析。在您的特定情况下,我可能会定义一个枚举

public enum Keywords {
    SettleLG,
    SettHjort,
    SkutteLG,
    SkuttHjort,
    RundBall    
}

然后你可以解析关键词

//Note this will (deliberately) throw an exception 
//if the string does not match a defined value
//depending on your needs you might want to handle that
var parsedKeyword = (Keywords)Enum.Parse(typeof(Keywords),keyword,true);

有了这个,你就可以写一个开关了。

private string GetRelativUrlFromKeyword(Keywords parsedKeyword){
    switch(parsedKeyword)
      case Keywords.SetteLG:
      case Keywords.SettHjort:
        return "../SMSFunction/SeenSMS.ascx";
      case Keywords.SkutteLG:
      case Keywords.SkuttHjort:
        return "../SMSFunction/ShotSMS.ascx";
      case KeyWords.RundBall:
        return "../SMSFunction/RoundballSMS.ascx";
      default:
        throw new InvalidOperationException("Keyword not recognized" + parsedKeyword);
}

将它们放在一起,您的调用代码将如下所示

var parsedKeyword = (Keywords)Enum.Parse(typeof(Keywords),keyword);
var relativeUrl = GetRelativUrlFromKeyword(parsedKeyword);
Response.Redirect(relativeUrl,true);

通过将值解析为枚举,您还可以验证值 (1),这样可以更轻松地查找与传入错误值的代码的其他部分相关的错误。如果您希望在一组值和另一组值之间有一个硬编码的映射,就像在关键字和相对 URL 之间一样,Switch 构造可以正常工作。

我将映射拆分到一个单独的函数中,因为它使代码更容易推理每个函数/方法何时做一件事。交换机中的默认情况将引发异常。这是当您添加新关键字但忘记在切换中处理它时捕获的。您可以选择其他默认值。我通常喜欢在默认情况下抛出异常,因为很可能是因为我在更改代码的其他部分时忘记做某事。(2). 我还在你的 response.redirect 中添加了一个 bool (true),它告诉框架你已经完成了响应,因此它可以将其发送给客户端(这很小,但我更喜欢让我的代码尽可能明确,当涉及到代码的意图时。

(1)如果您无法获得关键字字符串,请告诉我,我可以展示您将如何使用TryParse(2)如果我遗漏了一个可能的大小写值,我实际上希望有一个编译器警告,就像我在F#中有一个不完整的模式匹配一样,但这不一定是开关(例如,当使用字符串时,它不是)