-3

I have a form with a button, when the button is clicked, the [public int m = 0] changes 1. I have another form [form2], when I make a new class from form1 to get m value, it passes 0 and I don't know why.

public partial class Form1 : Form
{
    public int m =0;

    private void button1_Click(object sender, EventArgs e)
    {

        m = 1;

    }

}

public partial class Form2 : Form
{

Form2 form2_new = new Form2();

// methods to get m ---------> it should be 1 but I get 0!!!!

}
Tieson T.
  • 20,774
  • 6
  • 77
  • 92
Sina
  • 5
  • 1
  • 4
  • There is a wealth of Q&A on this topic already. This post is surely a duplicate of at least one, if not many, of those here: http://stackoverflow.com/search?q=%5Bc%23%5D+pass+data+between+two+forms – Peter Duniho Nov 04 '14 at 04:24
  • google is your best friend. There are literally tons of the same question with answers. – deathismyfriend Nov 04 '14 at 04:24
  • possible duplicate of [How to make a variable that will work on multiple forms?](http://stackoverflow.com/questions/24337534/how-to-make-a-variable-that-will-work-on-multiple-forms) – Shadow Nov 04 '14 at 04:30

1 Answers1

1

Inside Form2 make a new constructor:

public int m;
public Form2(int m)
{
    InitializeComponent();

    this.m = m;
}

And then from Form1 call Form2 and pass the value:

private void button1_Click(object sender, EventArgs e)
{
    m = 1;

    Form2 frm2 = new Form2(m);
    frm2.Show();
}
Josef
  • 2,648
  • 5
  • 37
  • 73