0

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;
            }
        }
    }
8
  • Thanks, I forgot about that. I just added it. Commented Dec 15, 2016 at 1:35
  • So if your string contain "x=14" for example, you want to assign 14 to your x variable? Commented Dec 15, 2016 at 1:40
  • 2
    Do you have to have separately named variables? Seems like it'd make more sense to store the results in a Dictionary<string, int> since they're key value pairs. Commented Dec 15, 2016 at 1:42
  • In fact, I have a class myObject, and those are properties of the object, like Color, Height, Weigth, etc. They have a meaning for me. Some of the values are strings (such as "red", while others are integers or floats. Commented Dec 15, 2016 at 1:45
  • 3
    Read up on Deserialization. You might wanna use built-in ones like JSON instead if you don't necessarily need to use the current format. Commented Dec 15, 2016 at 1:49

2 Answers 2

2

Why does the variable names matter? You can just store the pairs in a

Dictionary<string, int>



Dictionary<string, int> values= new Dictionary<string, int>()
{
    { a, 1}
    { b, 2}
};

The when you want to retrieve

var value = values[myString].value;
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the variable names have a meaning for me (they are properties of an object), so when I build the string to be analyzed, I fill in the relevant properties in it.
1

You can do this:

void Test(string myString)
{
    int a, b, c, d;
    a = b = c = d = 0;

    var assign = new Dictionary<string, Action<int>>()
        {
            { "a", n => a = n },
            { "b", n => b = n },
            { "c", n => c = n },
            { "d", n => d = n },
        };

    string[] varNames = { "a", "b", "c", "d" };
    for (int i = 0; i < varNames.Length; i++)
    {
        if (myString.IndexOf(varNames[i]) >= 0)
        {
            assign[varNames[i]](3);
        }
    }
}

But it is still very similar to a case statement. The only difference is that you build the assignments at run-time so you have more flexibility in how to create the dictionary - i.e. built across multiple places in your code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.