How to combine two lines in the name of another line and call this line?

Is there a way to simplify this code in a few lines, I have a class with the string seq (number) that has {get; set}. I get lucio and textValue from another class.

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; } } 
+2
source share
4 answers

Perhaps this will help you reduce 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; } } 
+1
source

Perhaps this code will suit you:

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; }

0
source

Just use the dictionary

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

then set the entry in the dictionary

 lookup[lucio] = textValue; 

and access it

 Console.WriteLine(lookup[lucio]) 

if you really want to check that the string has a value from 0 to 15, then first analyze it and check it and use a dictionary with an integer as the key

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

Hope this helps

 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; } } } 
0
source

Source: https://habr.com/ru/post/1342627/


All Articles