-1

Possible Duplicate:
Cloning objects in C#

My code :

private void button1_Click(object sender, EventArgs e)
{
    CopyForm(new Form1());
}

public void CopyForm(Form form)
{
    Form frm = form;
    frm.Text = "1";
    form.Text = "2";

    string c = frm.Text;// out 2
    string c2 = form.Text;// out 2
}

How to create object from Form without Ref ?

Please show me the best way.

Edit :

please sample.

Community
  • 1
  • 1
  • 2
    Have a look here http://stackoverflow.com/questions/78536/cloning-objects-in-c-sharp – JohnnBlade Jul 31 '12 at 06:12
  • Not sure what it is you are asking. What do you mean with 'how to create object from Form without Ref'? – Maarten Jul 31 '12 at 06:19
  • create object from Form without Ref. – user1329714 Jul 31 '12 at 06:24
  • @Maarten Typically when form (designer) variables are referenced, they will be treated as a reference, since these are allowed to be referenced many ways. Using a clone, like JohnnBlade suggests will create a new instance. Theoretically this allows the activator to copy this instance. – jamesbar2 Jul 31 '12 at 06:26

1 Answers1

2

You can maka copy of Form using Copy Constructor or using ICloneable's Clone method.

Given below is a simple example for Copy Constructor.You need to create you own form class and add a copyConstructor method to it.

class MyClonableForm:Form
{

public MyClonableForm(Form oldForm)//Copy Constructor
{
this.Text=oldForm.Text;
//write your clone code here
//be careful with reference types!
}

}

Note:

The use of ICloneable interface is not recommended because it does not specify the type of clone performed i.e. deep or shallow.

If you do want to use it,you can but dont expose the method Clone publicly.Use it for your own purpose!

Anirudha
  • 32,393
  • 7
  • 68
  • 89