I want to have an ArrayList where it contains HashTables. I create a Hashtable and added values. I then added it to the ArrayList. I then changed the values of the Hashtables and added it again the Array List. It does not save the first values and ending having duplicated values which were exactly the same as the last values!
Any suggestions? Here is my code
namespace ValuesTest
{
internal class Class1
{
public static ArrayList StartList = new ArrayList();
public static Hashtable Start = new Hashtable();
static void Main(string[] args)
{
Start["active"] = true;
Start["name"] = "prog1";
Start["path"] = @"C:\programfiles\prog1";
Start["parameter"] = string.Empty;
StartList.Add(Start);
Start["active"] = false;
Start["name"] = "prog2";
Start["path"] = @"C:\programfiles\prog2";
Start["parameter"] = "/q";
StartList.Add(Start);
foreach (Hashtable HT in StartList)
{
Console.WriteLine(HT["active"] + " - " + HT["name"] + " - " + HT["path"] + " - " + HT["parameter"]);
// it will always gives
// False - prog2 - C:\programfiles\prog2 - /q
}
Console.ReadLine();
}
}
}
Move start to inside your Main function, and reinitialize it for the second add(and as @lasseespeholt said use
List<T>).