I have a list of type Person. When I create the list of people it replaces the previous list element's information with the current one. I have read about this being the problem with static class variables but none of my attributes are static.
class Person{
private string _name;
private string _address;
public string Name{
get{ return _name;}
set { _name = value;}
public string Address{
get{ return _address;}
set { _address = value;}
}
}
I read in the people from a file and store it in a array of string. I stepped through to make sure the array is correct. It is.
Here is where it gets weird:
string[] personArray;
Person tempPerson = new Person();
List<Person> people = new List<Person>();
foreach (string line in lines)//lines are the people from file, it is correct
{
personArray = line.Split(',');
if (personArray.Length == 2)
{
tempPerson.Name = personArray[0];
tempPerson.Address = personArray[1];
people.Add(tempPerson);
}
}
I step through the code and the first person is added properly, the second person is added look into people and they both have the second person's information. Everything looks right until after the add statement.