C# windows form project, public variable in one class gets lost in another class on button click

Gennady Gurin 141 Reputation points
2025-06-01T22:22:29.41+00:00

I declare variable in one class like this:

public ushort variableA = 0;

then I have 2 different buttons, on click I assign

in a first button : variableA =1;

in a second burton: variableA = 2;

then on those buttons click I open a new form.

then I declare in another class in this new form:

First_Form My_variableA= new First_Form();

Then : if (My_variableA.variableA == 1){ do something}

       if (My_variableA.variableA == 2) {do something  }
```I have tested , in MessageBox.Show the value is still 0, even though I make assignment to 1 or 2,

not sure why the public variable value on button click does not get modified???

Get back, thanks in advance.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,549 questions
0 comments No comments
{count} votes

Accepted answer
  1. SurferOnWww 4,636 Reputation points
    2025-06-02T00:25:54.9433333+00:00

    I understand that you defined the variableA like below:

    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class First_Form : Form
        {
            public ushort variableA = 0;
    
            public First_Form()
            {
                InitializeComponent();
            }
        }
    }
    

    then I declare in another class in this new form:

    First_Form My_variableA= new First_Form();

    Please note that new First_Form() will create new instance of First_Form class in which the variable variableA is initialized with value of 0.

    That's why you observed:

    the public variable value on button click does not get modified???

    To resolve the issue, I suggest that you change the variableA to static as shown below:

    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class First_Form : Form
        {
            public static ushort variableA = 0;
    
            public First_Form()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, System.EventArgs e)
            {
                variableA = 1;
                var new_Form = new New_Form();
                new_Form.Show();
            }
    
            private void button2_Click(object sender, System.EventArgs e)
            {
                variableA = 2;
                var new_Form = new New_Form();
                new_Form.Show();
            }
        }
    }
    

    and refer the variableA in new Form as follows:

    using System.Windows.Forms;
    
    namespace WindowsFormsApp1
    {
        public partial class New_Form : Form
        {
            public New_Form()
            {
                InitializeComponent();
    
                this.label1.Text = "First_Form.variableA: " + First_Form.variableA;
            }
        }
    }
    

    The result is:

    enter image description here


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.