I have two forms (Form1 and Form2). On Form1 there is a ‘public int i’ variable, which is set to value 1 in Form1 constructor.
Then I open Form2 from Form1 with this code:
Form2 f2 = new Form2(ref i);
f2.ShowDialog();
The constructor of Form2 looks like this:
public int i;
public Form2(ref int x)
{
InitializeComponent();
i = x;
}
Then I set variable i on Form2 to value 2 and close Form2. Now I would expect the variable i on Form1 to have value 2 (because of ‘ref’ keyword by passing parameters), but the value is still 1. What am I doing wrong and why is ref keyword not working in my example?
Thanks
What’s actually happening.
There are many ways to accomplish what you want, either with events, or by passing in an object that will be shared between object1 and object2.
This would probably be the easiest way to accomplish what you’re going for.