I have a Dictionary that when I add multiple values to it, the items that were entered before take the values of the item added. I am using the .Net 3.5 Here is the code:
public static Dictionary<string, Neighborhoods> Families() { if (File.Exists(calculatePath() + 'Family.txt')){} else {File.Create(calculatePath() + 'Family.txt').Close();} string[] inp = File.ReadAllLines(calculatePath() + 'Family.txt'); Neighborhoods temp = new Neighborhoods(); Dictionary<string, Neighborhoods> All_Families = new Dictionary<string, Neighborhoods>(); string currentphase = null; foreach (string s in inp) { switch (s) { case '!<Start Family>!': temp = new Neighborhoods(); break; case '<Family Name>': currentphase = '<Family Name>'; break; case '<End Family Name>': currentphase = null; break; case '<Neighbour Enabled>True': temp.Neighbourhood_Enabled1 = true; currentphase = '<Neighbour Enabled>True'; break; case '<Neighbour Enabled>False': temp.Neighbourhood_Enabled1 = false; temp.Neighbourhood_Input1 = null; break; case '<University Enabled>True': temp.University_Enabled1 = true; currentphase = '<University Enabled>True'; break; case '<University Enabled>False': temp.University_Enabled1 = false; temp.University_Input1 = null; currentphase = null; break; case '<Downtown Enabled>True': temp.Downtown_Enabled1 = true; currentphase = '<Downtown Enabled>True'; break; case '<Downtown Enabled>False': temp.Downtown_Enabled1 = false; temp.Downtown_Input1 = null; currentphase = null; break; case '!<End Family>!': All_Families.Add(temp.Name, temp); break; default: if (currentphase == '<Family Name>') temp.Name = s; if (currentphase == '<Neighbour Enabled>True') temp.Neighbourhood_Input1 = s; if (currentphase == '<University Enabled>True') temp.University_Input1 = s; if (currentphase == '<Downtown Enabled>True') temp.Downtown_Input1 = s; break; } } return All_Families; }
How can I make it so that when I add new keys and values, the old keys keep their original value
Sample data:
!<Start Family>! Family Name> qwe <End Family Name> <Neighbour Enabled>True qwe <University Enabled>True we <Downtown Enabled>True qwe !<End Family>! !<Start Family>! <Family Name> 123 <End Family Name> <Neighbour Enabled>True 123 <University Enabled>True 123 <Downtown Enabled>True 123 !<End Family>!
Here is the nieghbourhoods class for reference. I will try the xml methods but it wont be finished quickly, I’m still learning this stuff.
class Neighborhoods { public Neighborhoods() { name = ''; Neighbourhood_Enabled = false; Neighbourhood_Input = ''; University_Enabled = false; University_Input = ''; Downtown_Enabled = false; Downtown_Input = ''; } static string name; public string Name { get { return Neighborhoods.name; } set { Neighborhoods.name = value; } } static bool Neighbourhood_Enabled; public bool Neighbourhood_Enabled1 { get { return Neighborhoods.Neighbourhood_Enabled; } set { Neighborhoods.Neighbourhood_Enabled = value; } } static string Neighbourhood_Input; public string Neighbourhood_Input1 { get { return Neighborhoods.Neighbourhood_Input; } set { Neighborhoods.Neighbourhood_Input = value; } } static bool University_Enabled; public bool University_Enabled1 { get { return Neighborhoods.University_Enabled; } set { Neighborhoods.University_Enabled = value; } } static string University_Input; public string University_Input1 { get { return Neighborhoods.University_Input; } set { Neighborhoods.University_Input = value; } } static bool Downtown_Enabled; public bool Downtown_Enabled1 { get { return Neighborhoods.Downtown_Enabled; } set { Neighborhoods.Downtown_Enabled = value; } } static string Downtown_Input; public string Downtown_Input1 { get { return Neighborhoods.Downtown_Input; } set { Neighborhoods.Downtown_Input = value; } } }
With the sample data you’ve given and the code you’ve given, it works okay using a
Neighborhoodsclass like this:My test is to run this code:
That prints out ‘qwe’ and ‘123’ – showing that there are two different objects involved.
However, we haven’t seen the real
Neighborhoodsclass yet. I don’t suppose it’s using static fields (but still instance properties) is it? That would certainly explain the behaviour you’re seeing.EDIT: Yup, now you’ve shown us the Neighborhoods code it makes sense. Those fields are meant to be relevant for each instance, not just the type itself – so they shouldn’t be static.
To show this is nothing to do with the parser, try this:
You’ll see it prints out ‘First’ – which is clearly not what you want!
Unfortunately I don’t have a good page about what ‘static’ means, but I suggest you look it up in whatever C# books you have.