0

I've been having this issue with a Dictionary in C#. Whenever I add an entry, all the entries in the dictionary are filled with the same value. Here's the code:

using System;
using System.Collections.Generic;

namespace TESTING
{
    class Program
    {
        /*--------------------Variable Declaration------------------------*/
        public static int NumberOfPlayers = 5;
        public static Dictionary<int, bool> PlayerAI = new Dictionary<int, bool>();
        public static Dictionary<int, Dictionary<string, int>> Players = new Dictionary<int, Dictionary<string, int>>();

        /*----------------------------------------------------------------*/
        public static void Main(string[] args)
        {
            Dictionary<string, int> TMP = new Dictionary<string, int>();
            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Console.WriteLine(i);
                TMP.Clear();
                TMP.Add("Player Target", 0 + (i * 3));
                TMP.Add("Player Cash", 0 + (i * 3));
                TMP.Add("Player Savings", 0 + (i * 3));
                TMP.Add("Borrow Interest", 0 + (i * 3));
                Console.WriteLine(i);
                Players.Add(i, TMP);
                Console.WriteLine(i);
            }

            //----------------------------DEBUG
            for (int i = 0; i < NumberOfPlayers; i++)
            {
                Console.WriteLine(i);
                Dictionary<string, int> PVT = new Dictionary<string, int>();
                PVT = Players[i];
                Console.WriteLine(PVT["Player Target"]);
                Console.WriteLine(PVT["Player Cash"]);
                Console.WriteLine(PVT["Player Savings"]);
                Console.WriteLine(PVT["Borrow Interest"]);

            }
            //------------------------------------

            Console.ReadKey();
        }
    }
}

` And here is the output:

Output

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Armature
  • 37
  • 7
  • 2
    Dictionaries are reference types. when you change `TMP` you change all the references, not just the one in that particular loop – Liam Feb 13 '18 at 16:05
  • Well, you have a reference in your dictionary to the TMP dictionary, if you would recreate it in the loop where you are adding it to your players, you wouldn't have this problem – Icepickle Feb 13 '18 at 16:06
  • 3
    Related [What is the difference between a reference type and value type in c#?](https://stackoverflow.com/questions/5057267/what-is-the-difference-between-a-reference-type-and-value-type-in-c) – Liam Feb 13 '18 at 16:07

2 Answers2

7

You have only created one dictionary with

Dictionary<string, int> TMP = new Dictionary<string, int>();

when you add this dictionary to the Players, you add a reference to the dictionary. This means that all entries refer to the same dictionary.

To fix the problem, you need to create a new dictionary on each iteration of the loop:

for (int i = 0; i < NumberOfPlayers; i++)
{
    Console.WriteLine(i);
    Dictionary<string, int> TMP = new Dictionary<string, int>();
    TMP.Add("Player Target", 0 + (i * 3));

etc.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
1

You are using the same Dictionary in Players. The dictionary will only store references. You need to create a new Dictionary and store that reference.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445