My program class has:
Application.Run(new Form1());
in form1 class I have:
model = new Model(this);
private void userEnteredText()
{
Model clonedModel = (Model)model.Clone();
this.myButton.Size = new System.Drawing.Size(10,5);
MessageBox.Show("buttons made small");
this = clonedModel;
MessageBox.Show("clone complete and buttons restored to orig size");
}
in model class I have:
public Model(Form1 form1)
{
myform = form1;
}
public object Clone()
{
return new Model(myform);
}
My initial form1 object has the size of buttons really large. After the user enters a value in a textbox: I clone the model object and call a method that makes the buttons really small. How can I then set the model object to point back to the original model object with large buttons?
I’m getting this error:
“Cannot assign to this because it is read-only”
I know I can just change the button size but I need to clone the entire object because there are other original variables that I want reset.
One question – is it expected behaviour that whilst clone both models references the same
Form?Clone()method just creates a new instance of Model but it still reference the sameFormobject,You can persist state of the initial model in an other private field like
and before applying a user-defined values just backup and later restore current model like: