Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Winforms Get/Set between classes causing error – Object Reference Error

Having trouble solving this one. Might just be burned out tbh, ive been at this for hours. I am new to Classes in C# and it is kicking the crap out of me trying to pass data between classes. I know there are steps that I am missing, but microsoft docs is not being very helpful with my question so here goes.

Trying to pass values from once class to another. The error code I am getting is CS0120

This is the format of what i am using within the first class

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

private void btn_Compute_Click(object sender, EventArgs e)
    {
        decimal dL = Validator(box_Left.Text);
        decimal dR = Validator(box_Right.Text);
        decimal Answer = 0;
        string op = "";
        if (rad_Add.Checked == true)
        {
            MathFirstClass.Left = dL;
            MathFirstClass.Right = dR;
            op = " + ";
        }
}

and the code inside the other class that I am trying to send the data to looks like this

    decimal left;
    decimal right;
    decimal Answer;

    public decimal Left
    {
        get { return left; }
        set { left = value; }
    }

    public decimal Right
    {
        get { return right; }
        set { right = value; }
    }

    public decimal Add_Operands
    {
        get
        {
            Answer = Left + Right;
            return Answer;
        }
    }

Also if anyone wants to fill me in on how to send the answer back to the first class that would also be a great help.

>Solution :

You create an instance of your class.

private void btn_Compute_Click(object sender, EventArgs e)
{
        decimal dL = Validator(box_Left.Text);
        decimal dR = Validator(box_Right.Text);
        decimal Answer = 0;
        string op = "";
        
        //****************************************
        MathFirstClass mathFirstClass = new MathFirstClass();
        
        if (rad_Add.Checked == true)
        {
            mathFirstClass.Left = dL;
            mathFirstClass.Right = dR;
            op = " + ";
        }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading