I don't consider myself an inexperienced programmer nor a brilliant one however this little problem has stumped me. Envisage the simple program at the bottom outlining situation.
A brief explanation of the program:
1. When main form is opened it declares a 'window' and puts a new 'ObjectToTest' in the constructor
2. Upon opening the new modal form it assigns the object handed to the form to 2 variables named 'OriginalObject' and 'ModifiedObject'
3. I then modify 'ModifiedObject' by adding 9 to its list
4.When the modal form returns and shows the 2 MessageBoxs they both show the same value 9. Yet there source variable created is of different values.
My Question: Why is the MessageBox not showing the correct variable?:confused:
(My guess is this is something to do with pointers, something i'm not experienced with)
Thankyou to the people who clear this up for me :)
---------------------------------------------------
Main Form Code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string OriginalString = null;
string ModifiedString = null;
window TestWindow = new window(new ObjectToTest());
TestWindow.ShowDialog(this);
foreach (int OriginalNumber in TestWindow.GetOriginal().List)
{
OriginalString += " " + OriginalNumber;
}
foreach (int ModifiedNumber in TestWindow.GetModified().List)
{
ModifiedString += " " + ModifiedNumber;
}
MessageBox.Show("Original String = " + OriginalString);
MessageBox.Show("Modified String = " + ModifiedString);
}
}
My own testing class (Simple class with one list property):
public class ObjectToTest
{
private List<int> Something = new List<int>();
public List<int> List
{
set
{
Something = value;
}
get
{
return Something;
}
}
}
An empty form dialog box I created:
public partial class window : Form
{
ObjectToTest OriginalObject = new ObjectToTest();
ObjectToTest ModifiedObject = new ObjectToTest();
public window()
{
InitializeComponent();
}
public window(ObjectToTest ObjectToEdit)
{
InitializeComponent();
OriginalObject = ObjectToEdit;
ModifiedObject = ObjectToEdit;
ModifiedObject.List.Add(9);
}
public ObjectToTest GetOriginal()
{
return OriginalObject;
}
public ObjectToTest GetModified()
{
return ModifiedObject;
}
}