I have a class to hold test data. I have a main form that puts data into an instance of the test data class. I have another form with a 2D array of data class objects. The main form copies its test data object to an element of the array in the second form. Subsequent changes to the test data object in the main form are mirrored into the object in the 2D array - until the test data object is written to a different element of the array; leaving two array elements with the same data, one incorrect.
public class MATSData : ICloneable
{
private float _param;
public MATSData()
{
Clear();
}
public MATSData(MATSData source)
{
_param = source.Param;
}
public object Clone()
{
return (MATSData)MemberwiseClone();
}
public float Param
{ get { return _param; } set { _param = value; } }
public Clear()
{
_param = 0;
}
public Init(MATSData source)
{
_param = source.Param;
}
}
public partial class MATSWaferTestForm : Form
{
private MATSData TestData;
private WaferMapForm MapDisplay;
public MATSWaferTestForm()
{
InitializeComponent();
MapDisplay = new WaferMapForm();
TestData = new MATSData();
}
private void RunTestt()
{
TestData.Param = 42.7F;
MapDisplay.SetDieResult(1, 5, TestData);
//At this point LoadedData[1,5].Param = 42.7
TestData.Param = 13.2F;
//At this point LoadedData[1,5].Param = 13.2
MapDisplay.SetDieResult(1, 6, TestData);
//At this point LoadedData[1,5].Param = 13.2 and LoadedData[1,6].Param = 13.2
TestData.Param = 56.9F;
MapDisplay.SetDieResult(1, 7, TestData);
//Now, LoadedData[1,5].Param = 13.2
//and both LoadedData[1,6].Param and LoadedData[1,7].Param = 56.9
}
}
public partial class WaferMapForm : Form
{
private MATSData[,] LoadedData;
public WaferMapForm()
{
InitializeComponent();
for (int row = 0; row < MapRows; row++)
{
for (int column = 0; column < MapColumns; column++)
LoadedData[row, column] = new MATSData();
}
}
public void SetDieResult(int row, int column, MATSData diedata)
{
LoadedData[row, column].Init(diedata);
//LoadedData[row, column] = new MATSData(diedata);
//LoadedData[row, column] = (MATSData)diedata.Clone();
}
}
After searching for solutions, I tried copying by using the data class constructor; and then by making the data class ICloneable and using a Clone() method. In all cases, the last written element in the array of data objects mirrors subsequent changes to the single object in the main form - until a different array element is written. I can't see what I'm missing, I expect all three methods to copy the contents of TestData into one element of LoadedData without a lingering link.