I have searched for what could be causing the unexpected output (for me at least) and I haven't got very far. I think it has something to do with classes being a reference type, but I'm confused as you'll see the output below shows the strings as I expect however the contents of the array seems to be updating a reference rather then a value? I'm confused. I've stripped my code down to the following. Basically I have an unknown number of "players", with many more properties then I've shown here. Each "player" has pieces of "equipment", in actual fact there are always 24 pieces.
I've used an array here because I want to be able to compare the equipment for different players at the same index in the array.
public class Equipment
{
public int PropA { get; set; }
public int PropB { get; set; }
public Equipment ()
{
PropA = 0;
PropB = 0;
}
public Equipment (Equipment eq)
{
PropA = eq.PropA;
PropB = eq.PropB;
}
}
public class Player
{
public string FirstName { get; set; }
public string LastName { get; set; }
public Equipment[] Equip = new Equipment[2];
public Player ()
{
FirstName = "";
LastName = "";
for (int i=0; i<Equip.Length; i++)
{
this.Equip[i] = new Equipment();
}
}
public Player (Player p)
{
FirstName = p.FirstName;
LastName = p.LastName;
for (int i=0; i<p.Equip.Length; i++)
{
this.Equip[i] = p.Equip[i];
}
}
}
public void LoadPlayers()
{
Player tmpPlayer = new Player();
List<Player> PlayerList = new List<Player>();
tmpPlayer.FirstName = "One";
tmpPlayer.LastName = "Two";
tmpPlayer.Equip[0].PropA = 1;
tmpPlayer.Equip[0].PropB = 2;
tmpPlayer.Equip[1].PropA = 3;
tmpPlayer.Equip[1].PropB = 4;
PlayerList.Add(new Player(tmpPlayer));
tmpPlayer.FirstName = "Three";
tmpPlayer.LastName = "Four";
tmpPlayer.Equip[0].PropA = 5;
tmpPlayer.Equip[0].PropB = 6;
tmpPlayer.Equip[1].PropA = 7;
tmpPlayer.Equip[1].PropB = 8;
PlayerList.Add(new Player(tmpPlayer));
foreach (Player p in PlayerList)
{
Console.WriteLine(p.FirstName);
Console.WriteLine(p.LastName);
Console.WriteLine(p.Equip[0].PropA.ToString());
Console.WriteLine(p.Equip[0].PropB.ToString());
Console.WriteLine(p.Equip[1].PropA.ToString());
Console.WriteLine(p.Equip[1].PropB.ToString());
}
}
This is the output I get:
One Two 5 6 7 8 Three Four 5 6 7 8
This is the output I expected:
One Two 1 2 3 4 Three Four 5 6 7 8
Other than pointing out my error is there an obvious better way of achieving my goal?
Thank you in advance, I appreciate any and all help!