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: