is there any way to simplify this code into a few lines, I have a class with string seq(number) has {get;set} functions. I am getting 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; }
}
May be this could help you reducing the 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;
}
}
Maybe 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;
}
Just use a dictionary
var lookup = new Dictionary<string,string>();
then to set an entry in the dictionary
lookup[lucio] = textValue;
and to access it
Console.WriteLine(lookup[lucio])
if you really want to verify that the string is a value between 0 an 15 then parse it first and validate it and use a dictionary with integer as a 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;
}
I hope this might help
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;
}
}
}