I have a string myString that can contain a variable number of pairs separated by commas, such as: "a=1,b=2,c=3" or "c=5,b=4" or "t=12". I also have a set integer variables, named a, b, c, ..., z, all set to 0. I want to analyze myString and assign the values from string to each variable. I can only thing of an inefficient way of doing this, using switch and case (i.e. if myString contains 'a', extract value of 'a' and assign it to variable named 'a'). Any ideas of how to write better code for this operation?
void Test(string myString)
{
int a, b, c, d;
a = b = c = d = 0;
string[] varNames = {"a", "b", "c", "d"};
for(int i = 0; i < varNames.Length; i++)
{
if(myString.IndexOf(varNames[i]) >= 0)
{
VariableWhoseNameIs(varNames[i]) = 3;
}
}
}
Dictionary<string, int>since they're key value pairs.