Passing a Form reference to a new Form
-
I'm trying to pass a reference of Form1 to Form2 so I can change things on Form1 from Form2. Here is my logic: Form1
private void btnShow_Click(object sender, System.EventArgs e) { FormTwo = new Form2(ref this); FormTwo.Show(); }
Form2:// .. yes form one was declared up here somewhere as public public Form2(ref Form1 PassingForm) { InitializeComponent(); FormOne = PassingForm; }
error CS1605: Cannot pass '' as a ref or out argument because it is read-only i can read the error, .. but what would the proper logic be here? -
I'm trying to pass a reference of Form1 to Form2 so I can change things on Form1 from Form2. Here is my logic: Form1
private void btnShow_Click(object sender, System.EventArgs e) { FormTwo = new Form2(ref this); FormTwo.Show(); }
Form2:// .. yes form one was declared up here somewhere as public public Form2(ref Form1 PassingForm) { InitializeComponent(); FormOne = PassingForm; }
error CS1605: Cannot pass '' as a ref or out argument because it is read-only i can read the error, .. but what would the proper logic be here?You don't need 'ref', because a Form1 is already a reference type. The error occurs because you're trying to pass a reference to 'this', which cannot be changed. Personally I find reference languages confusing, because of the nature of aliasing and that there are no clues in the syntax indicating whether you're dealing with a value (which uses a shallow copy) or a reference (which merely copies the reference).