Hello, I'm new to C# and have a problem with argument passing. Every time I pass an object, get some values from it and modify them in a new object, the passed object changes.
Code:
ShipModel first = new ShipModel("1x4", new int[,] {{0,0},{0,1},{0,2},{0,3}});
Ship a1 = new Ship(5, 5, first, false);
Ship a2 = new Ship(5, 5, first, true);
Ship a3 = new Ship(5, 5, first, true);
The code above outputs:
Code:
Inherited... 1x4 @ 0:0 0:1 0:2 0:3
Before moving... 1x4 @ 0:0 0:1 0:2 0:3
After moving... 1x4 @ 5:5 5:6 5:7 5:8
Inherited... 1x4 @ 5:5 5:6 5:7 5:8
Before moving... 1x4 @ 5:5 5:6 5:7 5:8
After moving... 1x4 @ 10:10 10:11 10:12 10:13
Inherited... 1x4 @ 10:10 10:11 10:12 10:13
Before moving... 1x4 @ 10:10 10:11 10:12 10:13
After moving... 1x4 @ 15:15 15:16 15:17 15:18
The initial model is being changed every time a ship is created. the initial coordinates (which should be constant) increases with every adjust(); Tried sealing the model class, did not help.
Code:
public Ship(int xCoord, int yCoord, ShipModel m, bool rotate)
{
x = xCoord; this.y = yCoord;
// inherits coordinates and name from the model
this.coordinates = new List<int[]>(m.getCoords());
this.name = new String(m.getName().ToCharArray());
Console.WriteLine("Inherited... " + this.ToString());
if (rotate) this.rotate();
this.adjust();
}
private void adjust()
{
// moves the ship
Console.WriteLine("Before moving... " + this.ToString());
for (int i = 0; i < this.coordinates.Count; i++)
{
this.coordinates[i][0] += this.x;
this.coordinates[i][1] += this.y;
this.coordinates[i][2] = 0;
}
Console.WriteLine("After moving... " + this.ToString());
}
Code:
class ShipModel
{
private List<int[]> coordinates = new List<int[]>();
private String name;
public ShipModel() { }
public ShipModel(String name, int[,] coordinates)
{
this.name = name;
for (int i = 0; i < (coordinates.Length / 2); i++)
{
this.coordinates.Add(new int[] { coordinates[i, 0], coordinates[i, 1], 0 });
}
}
public String getName()
{
return this.name;
}
public List<int[]> getCoords()
{
return this.coordinates;
}
}