jsfelipearaujo jsfelipearaujo - 3 months ago 12
C# Question

Change values of dictionary C#

I create a dictionary (

Dictionary<string, bool> values
).

I create the function:

private Dictionary<string, bool> ChangeValues(int line, Dictionary<string, bool> values)
{
for (int i = 0; i < line; i++)
{

}

return values;
}


I want to change bool values according the number of line.

The table below is the result of a dictionary (only show the bool):

line 0 = values["A"][false]
values["B"][false]
values["C"][false]

line 1 = values["A"][false]
values["B"][false]
values["C"][true]

line 2 = values["A"][false]
values["B"][true]
values["C"][false]

line n...


How I can change the bool value according the number of line?

Thanks.

[update]

After some help, the function is done:

private Dictionary<string, bool> ChangeValues(int line, Dictionary<string, bool> values)
{
string[] keys = values.Keys.ToArray();

keys = (from k in keys orderby k descending select k).ToArray();

for (int i = 0; i < line; i++)
{
for (int j = 0, bit = 1; j < values.Count; j++, bit <<= 1)
{
values[keys[j]] = (i & bit) != 0;
}
}

return values;
}


Thanks to PeterDuniho

Answer

If I understand the question correctly, all you want is to be able to iterate all possible combinations of the flags stored in your dictionary. In addition, these flags correspond exactly to the bits in your loop variable i.

If so, then you can just use the bits from the loop variable directly to set the flag values in the dictionary. For a general solution, you do need to pass the key names as well, in the correct order according to the bits you want to set.

For example:

private Dictionary<string, bool> ChangeValues(int line, Dictionary<string, bool> values, string[] keys)
{
    for (int i = 0; i < line; i++)
    {
        for (int j = 0, bit = 1; j < keys.Length; j++, bit <<= 1)
        {
            values[keys[j]] = (i & bit) != 0;
        }

        // do something with values here
    }

    return values;
}

Call like this:

var result = ChangeValues(8, values, new [] { "C", "B", "A" });

Note that the above simply modifies the dictionary that was passed in. I infer from the way the question was asked — "I want to change bool values" — that this was your intent. However, if you actually want to return a whole new dictionary object, you can do so:

private Dictionary<string, bool> ChangeValues(int line, string[] keys)
{
    Dictionary<string, bool> values = new Dictionary<string, bool>();

    for (int i = 0; i < line; i++)
    {
        for (int j = 0, bit = 1; j < keys.Length; j++, bit <<= 1)
        {
            values[keys[j]] = (i & bit) != 0;
        }

        // do something with values here
    }

    return values;
}

Of course, in that case the values of the original dictionary are of no use, so there's no need to pass it in, so I've left it out of the parameter list for this version of the method.

If in fact you want to return a new dictionary but still need the old one for some reason (e.g. you want to do something with it, like copying values or something, which you failed to mention in your question), then you can combine the two examples above to suit your needs.

Comments