Home > other >  Winforms Get/Set between classes causing error - Object Reference Error
Winforms Get/Set between classes causing error - Object Reference Error

Time:11-22

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

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.

CodePudding user response:

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 = "   ";
        }
}
  • Related