I am working on this visual studio 2008 calculator in c#. The calculator seems to be working, but when I created the Calculator Class, now it does not compute. I can click the number buttons, and they show up, but when I hit equals I just get a zero.
Can someone help me to get this class right. I know I have done something wrong, but as a beginner I have no clue
main form code snippet of click_equals

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            
            answerNumber = Calculator.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);
            txtDisplay.Text = Calculator.GetDisplayText(answerNumber);           
        }

and the code for the Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

            public static string GetDisplayText(double answerNumber)
            {
                return answerNumber.ToString();
            }
    }
}

I've got a bit of a concern in the way the information is being passed to the calculator class and in the way the answer textbox is being populated.

First:

answerNumber = Calculator.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);

It appears as if you're passing answerNumber to the calculator to get back a value of answerNumber. Realistically the values to be passed to the calculator should be just firstNumber, secondNumber, arithmeticProcess (and the calculator class should be adjusted to accept those 3 inputs).

Second:

txtDisplay.Text = Calculator.GetDisplayText(answerNumber);

Again it looks as though you are passing the answer back to the calculator... for it to pass you back the answer. If anything, once the calculator has passed you back the answer (via Return answerNumber in the calculator class) you should be simply transferring this value to the textBox and not passing it right back to the calculator for it to pass back as a string.

Alternate method: Instead of using a return value on the Equals process, simply store the information in the variable answerNumber within the calculator class. Then, use the GetDisplayText() process to call the locally stored answerNumber variable within it's own class and return it to you in text form for the textBox.

So... basically... I would do it one of two ways:

private void btnEquals_Click(System.Object sender, System.EventArgs e)
{
    Calculator.Equals(firstNumber, secondNumber, arithmeticProcess);
    txtDisplay.Text = Calculator.GetDisplayText();
}

Where the Equals procedure in the calculator class takes 3 variables and stores the answer within the class (no Return) and GetDisplayText takes the class-stored variable, converts it to string and returns it for the textBox.

Or...

private void btnEquals_Click(System.Object sender, System.EventArgs e)
{
    txtDisplay.Text = Convert.ToString(Calculator.Equals(firstNumber, secondNumber, arithmeticProcess));
}

Eliminating the GetDisplayText procedure alltogether.

Hope this helps :) Please mark as solved if it resolves your issue.

your class seems to be working... I think the problem is on how you call the class.


private void btnEquals_Click(System.Object sender, System.EventArgs e)
{

Calculator x = new Calculator();
double answerNumber;
answerNumber = x.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);

txtDisplay.Text = Convert.ToString(x.GetDisplayText(answerNumber));

}

commented: Good attempt :) +1

Error 1 Member 'Project_12_1_Create_Basic_Calculator.Calculator.Equals(double, double, string, double)' cannot be accessed with an instance reference; qualify it with a type name instead


Error 2 Member 'Project_12_1_Create_Basic_Calculator.Calculator.GetDisplayText(double)' cannot be accessed with an instance reference; qualify it with a type name instead


If I make the change of
Calculator x = new Calculator();
answerNumber = x.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);
txtDisplay.Text = x.GetDisplayText(answerNumber);

The errors above are the ones I am getting

main form btnEquals_Click code

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator calculator = new Calculator();
            answerNumber = calculator.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);

            txtDisplay.Text = calculator.GetDisplayText(answerNumber);
         
        }

Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

            public static string GetDisplayText(double answerNumber)
            {
                return answerNumber.ToString();
            }
    }
}

The first suggestion results in this error message:

Error 1 Use of unassigned local variable 'answerNumber'

and here is the coding for it
main form btnEquals_Click

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            
            txtDisplay.Text = Convert.ToString(Calculator.Equals(firstNumber, secondNumber, arithmeticProcess));
         
        }

and the Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess)
        {
            double answerNumber;
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

           
    }
}

your class seems to be working... I think the problem is on how you call the class.


private void btnEquals_Click(System.Object sender, System.EventArgs e)
{

Calculator x = new Calculator();
double answerNumber;
answerNumber = x.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);

txtDisplay.Text = Convert.ToString(x.GetDisplayText(answerNumber));

}

I am still getting an error message, I posted in the forum, can you take a look and see what Ive done wrong. I really want to understand, but for some reason I am just very confused about this

using Project_12_1_Create_Basic_Calculator; //<---- type this

private void btnEquals_Click(System.Object sender, System.EventArgs e)
{

Calculator x = new Calculator();

double answerNumber; // <- typethis (assigned local variable 'answerNumber')

answerNumber = x.Equals(22, 23, "+");

txtDisplay.Text = Convert.ToString(x.GetDisplayText(answerNumber));

}

actually type using Project_12_1_Create_Basic_Calculator;

over

private btnEquals_Click(System.Object sender, System.EventArgs e)
{
}

still getting error messages....

i see..hmmmm did u input a value?
ex:

answerNumber = x.Equals(22, 23, "+");

can i see the error message?

I will attach the code here, It is like a windows calculator, you click the button which inputs the firstNumber, then click the arithmeticProcess such as addition, subtraction, etc then click the secondNumber, when you click the equals button, it is to return the answer from the Calculator class.....

The error messages are:


Error 1 Member 'Project_12_1_Create_Basic_Calculator.Calculator.Equals(double, double, string, double)' cannot be accessed with an instance reference; qualify it with a type name instead

and

Error 2 Member 'Project_12_1_Create_Basic_Calculator.Calculator.GetDisplayText(double)' cannot be accessed with an instance reference; qualify it with a type name instead


to avoid an overabundance of code, I will only put the btnEquals_Click code

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator x = new Calculator();
            double answerNumber;
            answerNumber = x.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber);
            txtDisplay.Text = Convert.ToString(x.GetDisplayText(answerNumber));
         
        }

and here is the Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

            public static string GetDisplayText(double answerNumber)
            {
                return answerNumber.ToString();
            }
    }
}

any help is appreciated, as I am a beginner at this, and very lost

Ok... I was probably somewhat confusing in how I worded it the last time so here it is step by step :)

First, your program class:

Calculator newCalc = new Calculator(); //Create instance of Calculator called newCalc

private void btnEquals_Click(System.Object sender, System.EventArgs e)
    {            
        txtDisplay.Text = Convert.ToString(newCalc.Equals(firstNumber, secondNumber, arithmeticProcess));         
    }

Then the Calculator class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

            /*public static string GetDisplayText(double answerNumber)
            {
                return answerNumber.ToString();
            }*/
    }
}

MOST of the stuff above the Equals procedure is unused in the way you call this class as all the variables being used in the Equals procedure are obtained by the calling of the function in the first place (ie: newCalc.Equals(firstNumber, secondNumber, arithmeticProcess);). The only needed components above the Equals function are the answerNumber declaration at the top and the constructor of:

public Calculator()
        {
        }

So... using the changes I've outlined here you should get the following result:

On clicking btnEquals you will pass local variables firstNumber, secondNumber and arithmeticProcess to the Equals procedure within the Calculator class which is represented by newClass. Equals procedure will determine the type of mathematical process needed and generate a double value of answerNumber which it will pass back to it's caller. The caller will then convert that value to a string and insert it into txtDisplay.Text.

Hope that clears it up a bit more for you :) Sorry if I was a bit confusing originally.

owkie give me a minute i'll figure this out..

Use of unassigned local variable 'answerNumber'

Which also reminds me... answerNumber needs to be defined as a double in your Program class as well, I assumed it was declared in the excluded portion of the Program class prior to the button click event. Tho in the example I provided a moment ago, this is not required as I take the result of the Equals procedure and apply it directly to the text box.

still getting the same error messages...
here is the modified code

Main Form

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator newCalc = new Calculator();
            txtDisplay.Text = Convert.ToString(newCalc.Equals(firstNumber, secondNumber, arithmeticProcess));
         
        }

Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess)
        {
            double answerNumber;
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

           
    }
}

As a side note, you did an excellent job declaring methods for getting and setting the values of your class variables.

However, this is really only necessary when:

  1. Your variables are "private" or otherwise not visible outside the class
  2. You have procedures to set/retrieve the variables

An example of using the methods you set would be:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        private string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;
        
        public Calculator()
        {
        }

        public void setVars(double firstNum, double secondNum, string arithProc)
        {
            ArithmeticProcess = arithProc;
            FirstNumber = firstNum;
            SecondNumber = secondNum;
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static void Equals()
        {
            
            if (ArithmeticProcess == "+")
            {
                AnswerNumber = FirstNumber + SecondNumber;
                
            }
             else if (ArithmeticProcess == "-")
            {
                AnswerNumber = FirstNumber - SecondNumber;
                
            }
            else if (ArithmeticProcess == "*")
            {
               AnswerNumber = FirstNumber * SecondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                AnswerNumber = FirstNumber / SecondNumber;
                
            }
        }

        public static string GetDisplayText()
        {
            return AnswerNumber.ToString();
        }
    }
}

make note of the CAPITAL letters vs the lower case letters. And then in the Program class:

Calculator newCalc = new Calculator();

    private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            newCalc.setVars(firstNumber, secondNumber, arithmeticProcess);
            newCalc.Equals();
            txtDisplay.Text = Calculator.GetDisplayText();           
        }

Basically in doing this you first set the variables, then perform the calculation on the variables, then use the GetDisplayText function to return the answer.

Both this, and my 2nd post above, should work for your needs but this one actually UTILIZES the methods you've created.

still getting the same error messages...
here is the modified code

Main Form

private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator newCalc = new Calculator();
            txtDisplay.Text = Convert.ToString(newCalc.Equals(firstNumber, secondNumber, arithmeticProcess));
         
        }

Calculator Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;
        
        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;            
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;
            
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
           {
               firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess)
        {
            double answerNumber;
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
             else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            else if (arithmeticProcess == "*")
            {
               answerNumber = firstNumber * secondNumber;
                
            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
                
            }
            return answerNumber;
        }

           
    }
}

You've double-declared answerNumber, try just copy/pasting my prior example :)

Which also reminds me... answerNumber needs to be defined as a double in your Program class as well, I assumed it was declared in the excluded portion of the Program class prior to the button click event. Tho in the example I provided a moment ago, this is not required as I take the result of the Equals procedure and apply it directly to the text box.

I hope I am not being frustrating to you, I have never had such an issue with a program before, I appreciate your patience with me.
Thank You

Sry, ok... try copy/pasting the code I gave you in this post directly.

It's all your original code with my modifications so you should be able to copy from top to bottom of the Calculator class code to completely overwrite your existing Calculator class code. Use the Toggle Plain Text option to give direct access to the code without the numbers being copied too.

Error 1 An object reference is required for the non-static field, method, or property 'Project_12_1_Create_Basic_Calculator.Calculator.arithmeticProcess'

I end up with 18 of these errors when I copied the code, it says it for arithmeticProcess, firstNumber, secondNumber and answerNumber

one sec... I want to try it in my VS and see where I buggered it before posting again lol

one sec... I want to try it in my VS and see where I buggered it before posting again lol

lol, maybe I have a bad version of vs.....

Ok... I debug'd using the following (because you need this info to see what I did):

  1. I created a blank form and populated it with 4x textBox
  2. textBox1 was renamed firstNumber
  3. textBox2 was renamed arithmeticProcess
  4. textBox3 was renamed secondNumber
  5. textBox4 was renamed txtDisplay
  6. I added 1 button and named it btnEquals

At this point I created the Calculator class (the only difference here from what you will need is the namespace)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testApp
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;

        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;

        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
            {
                firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public double Equals(double firstNumber, double secondNumber, string arithmeticProcess)
        {

            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;

            }
            else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;

            }
            else if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;

            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;

            }
            return answerNumber;
        }

        /*public static string GetDisplayText(double answerNumber)
        {
            return answerNumber.ToString();
        }*/
    }
}

And at this point in my form1.cs file I did the following:

Calculator newCalc = new Calculator(); //Create instance of Calculator called newCalc

        private void btnEquals_Click(object sender, EventArgs e)
        {
            double firstNum = Convert.ToDouble(firstNumber.Text);
            double secondNum = Convert.ToDouble(secondNumber.Text);
            string arithProc = arithmeticProcess.Text;
            txtDisplay.Text = Convert.ToString(newCalc.Equals(firstNum, secondNum, arithProc));
        }

When I tested it in debug mode with 12 in the 1st box, x in the 2nd box and 10 in the 3rd box and clicked the button, the 4th box was populated with 120.

I am wondering though, since you created one with 4 textboxes, would the code be different for mine,
Mine only has a Display textBox then there are 10 buttons consisting of 0,1,2,3,4,5,6,7,8,9 and 4 buttons consisting of *,/,-,+ and an Equals button. I am going to attach the whole program, i know it's a lot of code, but bear with me please

here is the full code for the main form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Project_12_1_Create_Basic_Calculator
{
    public partial class frmCalculator : Form
    {
        public frmCalculator()
        {
            InitializeComponent();
        }
        private string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;
        

        private void btnOne_Click_1(object sender, EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnOne.Text;
        }
        private void btnTwo_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
        }
        private void btnThree_Click(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnThree.Text;
        }
        private void btnFour_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnFour.Text;
        }
        private void btnFive_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnFive.Text;
        }
        private void btnSix_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnSix.Text;
        }
        private void btnSeven_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
        }
        private void btnEight_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnEight.Text;
        }
        private void btnNine_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + btnNine.Text;
        }
        private void btnDecimal_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = txtDisplay.Text + ".";
        }
        private void btnClear_Click_1(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = "0";
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            string str;
            int loc;

            if (txtDisplay.Text.Length > 0)
            {
                str = txtDisplay.Text.Substring(txtDisplay.Text.Length - 1);                
                loc = txtDisplay.Text.Length;
                txtDisplay.Text = txtDisplay.Text.Remove(loc - 1, 1);
            }

        }



        

        private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator newCalc = new Calculator(); //Create instance of Calculator called newCalc
            txtDisplay.Text = Convert.ToString(newCalc.Equals(firstNumber, secondNumber, arithmeticProcess, answerNumber));
        }

        private void btnAdd_Click(System.Object sender, System.EventArgs e)
        {
            firstNumber = Double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
            arithmeticProcess = "+";
        }

        private void btnSubtract_Click_1(System.Object sender, System.EventArgs e)
        {
            firstNumber = Double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
            arithmeticProcess = "-";
        }

        private void btnMultiply_Click(System.Object sender, System.EventArgs e)
        {
            firstNumber = Double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
            arithmeticProcess = "*";
        }

        private void btnDivide_Click(System.Object sender, System.EventArgs e)
        {
            firstNumber = Double.Parse(txtDisplay.Text);
            txtDisplay.Clear();
            arithmeticProcess = "/";
        }

        private void btnSqrt_Click(object sender, EventArgs e)
        {
            if (txtDisplay.Text.Length != 0)
            {
                firstNumber = System.Double.Parse(txtDisplay.Text);
                firstNumber = System.Math.Sqrt(firstNumber);
                txtDisplay.Text = firstNumber.ToString();                
            }
        }

        private void btnInverse_Click(object sender, EventArgs e)
        {
            if (txtDisplay.Text.Length != 0)
            {
                firstNumber = System.Double.Parse(txtDisplay.Text);
                firstNumber = 1 / firstNumber;
                txtDisplay.Text = firstNumber.ToString();                
            }
        }

        private void btnChangeSign_Click(object sender, EventArgs e)
        {
            {
                if (txtDisplay.Text.Length > 0)
                {
                    firstNumber = -1 * System.Double.Parse(txtDisplay.Text);
                    txtDisplay.Text = firstNumber.ToString();
                }
            }
        }

and again the Calculator Class code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Project_12_1_Create_Basic_Calculator
{
    public class Calculator
    {
        public string arithmeticProcess;
        public double firstNumber;
        public double secondNumber;
        public double answerNumber;

        public Calculator()
        {
        }

        public Calculator(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.FirstNumber = firstNumber;
            this.ArithmeticProcess = arithmeticProcess;
            this.SecondNumber = secondNumber;
            this.answerNumber = answerNumber;

        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
            {
                firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public static double Equals(double firstNumber, double secondNumber, string arithmeticProcess, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;

            }
            else if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;

            }
            else if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;

            }
            else if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;

            }
            return answerNumber;
        }

        /*public static string GetDisplayText(double answerNumber)
        {
            return answerNumber.ToString();
        }*/
    }
}

To illustrate the 2nd possibility where the actual methods and such are actually utilized (which I assume may be part of your assignment) I used the same form with 4 text boxes and 1 button but changed the Calculator class to this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace testApp
{
    public class Calculator
    {
        private string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;

        public Calculator()
        {
        }

        public void setVars(double firstNum, double secondNum, string arithProc)
        {
            ArithmeticProcess = arithProc;
            FirstNumber = firstNum;
            SecondNumber = secondNum;
        }

        public string ArithmeticProcess
        {
            get
            {
                return arithmeticProcess;
            }
            set
            {
                arithmeticProcess = value;
            }
        }

        public double FirstNumber
        {
            get
            {
                return firstNumber;
            }
            set
            {
                firstNumber = value;
            }
        }

        public double SecondNumber
        {
            get
            {
                return secondNumber;
            }
            set
            {
                secondNumber = value;
            }
        }

        public double AnswerNumber
        {
            get
            {
                return answerNumber;
            }
            set
            {
                answerNumber = value;
            }
        }

        public void Equals()
        {

            if (ArithmeticProcess == "+")
            {
                AnswerNumber = FirstNumber + SecondNumber;

            }
            else if (ArithmeticProcess == "-")
            {
                AnswerNumber = FirstNumber - SecondNumber;

            }
            else if (ArithmeticProcess == "*")
            {
                AnswerNumber = FirstNumber * SecondNumber;

            }
            else if (arithmeticProcess == "/")
            {
                AnswerNumber = FirstNumber / SecondNumber;

            }
        }

        public string GetDisplayText()
        {
            return AnswerNumber.ToString();
        }
    }
}

And then I changed the form1.cs to this:

Calculator newCalc = new Calculator(); //Create instance of Calculator called newCalc

        private void btnEquals_Click(object sender, EventArgs e)
        {
            double firstNum = Convert.ToDouble(firstNumber.Text);
            double secondNum = Convert.ToDouble(secondNumber.Text);
            string arithProc = arithmeticProcess.Text;
            newCalc.setVars(firstNum, secondNum, arithProc);
            newCalc.Equals();
            txtDisplay.Text = newCalc.GetDisplayText();
        }

Using the same test scenario (12 x 10) I got the same result of 120.

So either this or my immediately previous response should work... half of the problem that I was encountering was that you were using "static" procedures which was generating about 90% of the errors.

I am going to fix it and see what happens

In your code you had your methods for declaring your variables, in mine I didn't so that's why I drew the results directly from separate textboxes (just to test them).

In your case you don't need to additionally declare

double firstNum = Convert.ToDouble(firstNumber.Text);
double secondNum = Convert.ToDouble(secondNumber.Text);
string arithProc = arithmeticProcess.Text;

Within your button click as you already have them declared as part of the other processes (at least firstNumber and arithmeticProcess) however you do need to put the 2nd number value into the variable when the equals button is pressed in order to pass the value to the Calculator class.

2 new errors....

Warning 1 Field 'Project_12_1_Create_Basic_Calculator.frmCalculator.secondNumber' is never assigned to, and will always have its default value 0

and

Warning 2 The field 'Project_12_1_Create_Basic_Calculator.frmCalculator.answerNumber' is never used

>> Warning 1 Field 'Project_12_1_Create_Basic_Calculator.frmCalculator.secondNumber' is never assigned to, and will always have its default value 0 as I said above, when you hit the equals button you need to assign the current visible number to secondNumber first :)

>>Warning 2 The field 'Project_12_1_Create_Basic_Calculator.frmCalculator.answerNumber' is never used
Wherever you have answerNumber declared in your frmCalculator form just place a // before it to comment it out as this version of the code doesn't use it.

I see, about the secondNumber, but how do I get it to assign the current visible number the the secondNumber first?

Never mind, got it....Thank you so so much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.