0

I have this code to retrieve a value(integer array) out of a dictionary Then increment one of the elements inside the integer array based on the if statement it is in..

        Dictionary<string, int[]> ResultDic = new Dictionary<string, int[]>();
        if (TeamOnePoint > TeamTwoPoint)
        {
            ResultDic.TryGetValue(TeamOneResult, out OutOfDic);
            OutOfDic[0]++;
            OutOfDic[1]++;
            ////ResultDic.Remove(TeamOneResult);
            ////ResultDic.Add(TeamOneResult, OutOfDic);
            ResultDic[TeamOneResult] = OutOfDic;;
            ResultDic.TryGetValue(TeamTwoResult, out OutOfDic);
            OutOfDic[0]++;
            OutOfDic[2]++;
            ////ResultDic.Remove(TeamTwoResult);
            ////ResultDic.Add(TeamTwoResult, OutOfDic);
            ResultDic[TeamTwoResult] = OutOfDic;
            }

Now the problem I have is that evertime I read the modified OutOfDic array back into the dictionary into the value part where I specified the Key, every value in the dictionary is modified as well, and not just the key I specified. The commented part gives the same result as the non commented part. How do I fix this problem to only add the value to the specified key?

Marno vN
  • 145
  • 3
  • 10
  • 3
    _"read back into"_? You mean _write back into_? You're also incrementing two ints not one. Also, why do you use `TryGetValue` if you are sure that the key exists? You don't handle the case that a key is not present. – Tim Schmelter Jun 06 '16 at 12:55
  • Thank you for the answer. I read the teams (Eg. 6 teams) into the key of the array and make the value (int[] TheArray) 0.. Then the teams play. and everytime a team play. (teamOne Win. TeamTwo Lost). then the teamOne P(Play) and W(Win) must increment. TeamTwo P and L(Lost) must increment. that is why I TryGetValue. I want to get the value of the team played and increment those values. then write(meant write) them to specified value where key == TeamOne – Marno vN Jun 06 '16 at 13:36

1 Answers1

2

The behaviour you've described is only possible if you've added the same array to the dictionary multiple times. Since arrays are reference types every change will affect all values in the dictionay.

So instead of doing this(for example):

Dictionary<string, int[]> ResultDic = new Dictionary<string, int[]>();
int[] array = { 1, 2, 3, 4, 5 };
ResultDic.Add("TeamOne", array);
ResultDic.Add("TeamTwo", array);

You should do this:

int[] array = { 1, 2, 3, 4, 5 };
ResultDic.Add("TeamOne", array);
array = new int[] { 1, 2, 3, 4, 5 };
ResultDic.Add("TeamTwo", array);

Note that it's not necessary to re-assign the array to the dictionary for the same reason (it's a reference type). So you can remove these lines:

ResultDic[TeamOneResult] = OutOfDic;;
// ....
ResultDic[TeamTwoResult] = OutOfDic;
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • 1
    Might be worth noting that, for the same reason, setting the key-value to the modified array after modifying it is not necessary. – Charles Mager Jun 06 '16 at 13:05