【问题标题】:How to combine two strings into the name of another string and call that string?如何将两个字符串组合成另一个字符串的名称并调用该字符串?
【发布时间】:2017-01-26 05:34:04
【问题描述】:

有什么方法可以将这段代码简化为几行,我有一个带有字符串 seq(number) 的类有 {get;set} 函数。我正在从另一个班级获得 lucio 和 textValue。

public static void setCategorySeq(string lucio, string textValue)
    {
        if (lucio == "0") { seq0 = textValue; }
        else if (lucio == "1") { seq1 = textValue; }
        else if (lucio == "2") { seq2 = textValue; }
        else if (lucio == "3") { seq3 = textValue; }
        else if (lucio == "4") { seq4 = textValue; }
        else if (lucio == "5") { seq5 = textValue; }
        else if (lucio == "6") { seq6 = textValue; }
        else if (lucio == "7") { seq7 = textValue; }
        else if (lucio == "8") { seq8 = textValue; }
        else if (lucio == "9") { seq9 = textValue; }
        else if (lucio == "10") { seq10 = textValue; }
        else if (lucio == "11") { seq11 = textValue; }
        else if (lucio == "12") { seq12 = textValue; }
        else if (lucio == "13") { seq13 = textValue; }
        else if (lucio == "14") { seq14 = textValue; }
        else if (lucio == "15") { seq15 = textValue; }
    }

【问题讨论】:

  • 我实际上不确定我是否理解您的问题。但我认为您正在寻找的是您可以调用一个键(例如 lucio)并且您想要设置其各自的值(例如 seq)。我说的对吗?
  • 你应该使用 Switch 语句来完成这种工作。
  • 正如@UmairAnwaar 所提到的,switch 将是这种情况下的最佳选择,虽然我个人喜欢缩短行数,所以我会研究反射(不确定是否会更好)到“设置”一个变量的值,基于其名称作为字符串。供参考:stackoverflow.com/a/5218649/6741868
  • @KeyurPATEL 您可以通过反射设置属性,但不能通过反射设置局部变量。对于用户正在尝试做的事情,字典可能是最好的解决方案。
  • @bradgonesurfing:太好了——这肯定会有所帮助。

标签: c# string class


【解决方案1】:

也许这可以帮助您减少 LOC。

    public static void setCategorySeq(string lucio, string textValue)
    {
        string[] seq = new string[16];
        for (int i = 0; i <= 15; i++)
        {
            if (lucio == i.ToString())
                seq[i] = textValue;
        }
    }

【讨论】:

  • 谢谢,这正是我要找的
【解决方案2】:

也许这个代码会适合你:

static Dictionary<string, string> seq = new Dictionary<string, string>(); public static void setCategorySeq(string lucio, string textValue) { seq[lucio] = textValue; } public static string setCategorySeq(string lucio) { if (seq.ContainsKey(lucio)) return seq[lucio]; return null; }

【讨论】:

    【解决方案3】:

    只要用字典

    var lookup = new Dictionary<string,string>();
    

    然后在字典中设置一个条目

    lookup[lucio] = textValue;
    

    并访问它

    Console.WriteLine(lookup[lucio])
    

    如果你真的想验证字符串是 0 到 15 之间的值,那么首先解析它并验证它并使用一个以整数为键的字典

    var lookup = new Dictionary<int,string>();
    
    // Using C# 7 notation
    if(int.TryParse(lucio, var out lucioInt) && lucionInt > 0 && lucioInt < 16){
        lookup[lucioInt] = textValue;
    }
    

    【讨论】:

      【解决方案4】:

      我希望这可能会有所帮助

      public static void setCategorySeq(string lucio, string textValue)
      {
      var seq = new string[15];
      int noOfsequence=15;
      for(int i=0;i<noOfsequence;i++)
          {
              if(lucio==i.ToString())
              {
               seq[i] = textValue;
               break;
              }
      
          }
      
      
      
       }
      

      【讨论】:

        猜你喜欢
        • 2017-10-25
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-25
        • 2013-05-24
        相关资源
        最近更新 更多