none
Access Variable From Different Class RRS feed

  • Question

  • How do I pass the value of tboxValue to my method EE?  It always prints 0?

    public partial class Form1 : Form
    {
    
        public double tboxvalue;
        private string exportdata;
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void btnClicker_Click(object sender, EventArgs e)
        {
            Functions.EE();
        }
        private void txtData_CheckedChanged(object sender, EventArgs e)
        {
            bool @checked = ((CheckBox)sender).Checked;
    
            if (@checked.ToString() == "True")
            {
                exportdata = "Yes";
                tboxvalue = Convert.ToDouble(this.txtData.Text);
                System.Diagnostics.Debug.WriteLine(tboxvalue.ToString());
            }
            else
                exportdata = "No";
        }
    }
    class Functions
    {
        public static void EE()
        {
            Form1 f1 = new Form1();
            System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
        }
    }

    Saturday, September 29, 2018 7:25 PM

All replies

  • Hi,

    with new you are creating a new instance. The value of tboxvalue is 0 because it' 0 for a new instance. You changed tboxvalue of the instance calling EE.

        public static void EE(Form1 f1)
        {
            System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
        }
        private void btnClicker_Click(object sender, EventArgs e)
        {
            Functions.EE(this);
        }

    You have to pass the correct instance to the static method instead of creating a new one.

    Greetings, Chris






    Saturday, September 29, 2018 7:59 PM
  • > How do I pass the value of tboxValue to my method EE?

    Value of tboxValue is a double type converted from txtData.Text.

    I didn't see any calculation with Double tboxvalue - so, it not needed.

    You can define your function as 

    class Functions
    {
        public static void EE(string myText)
        {
            System.Diagnostics.Debug.WriteLine(myText);
        }
    }

    And implement a call as :

        private void btnClicker_Click(object sender, EventArgs e)
        {
            Functions.EE(txtData.Text);
        }


    Sincerely, Highly skilled coding monkey.

    Saturday, September 29, 2018 9:40 PM
  • > It always prints 0?

    Within EE() you create new instance of the class Form1.

    class Functions
    {
        public static void EE()
        {
            Form1 f1 = new Form1();
            System.Diagnostics.Debug.WriteLine(f1.tboxvalue.ToString());
        }
    }

    Variables in this instance of the class have default or assigned in the constructor values, Default value for a Double type is 0. As you didn't fill txtData in this instance with any data and didn't call txtData_CheckedChanged value remain as zero.


    Sincerely, Highly skilled coding monkey.

    Saturday, September 29, 2018 9:46 PM
  • Variables in this instance of the class have default or assigned in the constructor values...


    Sincerely, Highly skilled coding monkey.

    Clearly, you are not skilled enough, because, instance variables can be initialized, i.e., can  have values different from default.

    You missed some training sessions...

    Saturday, September 29, 2018 10:35 PM