Click here to Skip to main content
16,012,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
I'm trying to send a parameter from one form to another as follows
C#
int number=0;
Form2 f2 = new Form2(ref number);
f2.ShowDialog();

C#
int nb;
       public Form2(ref int number)
       {
           nb=number;
           InitializeComponent();
       }



the problem is that when the user closes Form2 the integer "number" does not change
in Form2 i did some calculation and saved the result in "nb"
so what's the problem?
when i used a ref Label and saved the result in the label's text the result was what i wanted
but why is it not working with integers or strings?
thx in advance
Posted
Updated 10-Jan-12 10:55am
v2

Consider using for example properties instead of ref parameters (especially to constructors).

For example define the number as follows in form2:
C#
public number { get; set; }
public Form2()
{
    InitializeComponent();
}

Now you can refer the property from the calling side:
C#
int number=0;
Form2 f2 = new Form2();
f2.number = number;
if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
   number = f2.number;
}

The code above also checks that the dialog is closed with proper result and only if it is, the number at calling side is changed.

In order for this to work the number should also be changed somewhere in form2
 
Share this answer
 
Comments
Member 8403770 11-Jan-12 12:53pm    
ty :)
You answered your own question. You changed nb, not number. Before you close Form2, ensure that you have assigned the new value to number as well.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900