Basic Calculator in C#2008 Calculator Class question

Calling GetDisplayText() from the Calculator Class

Page 1 of 1

11 Replies - 2368 Views - Last Post: 08 July 2010 - 11:16 AM Rate Topic: -----

#1 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 09:41 AM

I have managed to get this calculator to work for the most part, the square root, inverse and 1/X button still are not calculating properly. My question is this, I created a class named Calculator, in which I will call the answer from the Equals in the form. However, it is not calling the code, Help, what am I doing wrong here.
 private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            txtDisplay.Text = Calculator.GetDisplayText();
        }


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
    {
        string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;
        
        public Calculator()
        {
        }

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

        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(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
            }
            if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
            }
            if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;
            }
            if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
            }
        }

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



Is This A Good Question/Topic? 0
  • +

Replies To: Basic Calculator in C#2008 Calculator Class question

#2 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 09:59 AM

Well, when you click the equals button...shouldn't some sort of calculation be done?
Right now you're just calling the GetDisplayText() method.....and in that method you're just returning the "answerNumber.ToString()". But you haven't set the answerNumber yet. Perhaps you should consider calling the Calculator's Equals(...) method at some point????

-Frinny
Was This Post Helpful? 0
  • +
  • -

#3 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:08 AM

calling the equals method might help..... :bigsmile:
I changed the Equals in the Calculator class to GetEquals, and changed the code in the main form

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



Calculator class
public double GetEquals(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
            if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;
                
            }
            if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
            }
            return answerNumber;
        }

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


Was This Post Helpful? 0
  • +
  • -

#4 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:11 AM

:) Does it work now?
Was This Post Helpful? 0
  • +
  • -

#5 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:13 AM

No, I keep getting these two errors:

Error 1 An object reference is required for the non-static field, method, or property 'Project_12_1_Create_Basic_Calculator.Calculator.GetEquals(string, double, double, double)'

and

Error 2 An object reference is required for the non-static field, method, or property 'Project_12_1_Create_Basic_Calculator.Calculator.GetDisplayText()'

unfortunately I don't understand how to fix this.
Was This Post Helpful? 0
  • +
  • -

#6 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:22 AM

Well your your Equals method has the following signature:
 public static double Equals(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)


That means that you need to pass a string and 3 doubles into the method to make it work.
So, in your equals button click event you need to create a string and 3 doubles before you can call the method:
string arithmenticProcess = "+";
double firstNumber = 1.2;
double secondNumber = 5.8;
double answerNumber = 0;

double theResult = Calculator.GetEquals(arithmeticProcess, firstNumber, secondNumber, answerNumber);
txtDisplay.Text = Calculator.GetDisplayText();


Since you are probably not looking to hard code this, you will want to substitute the hard coded values with something that the user provided via TextBoxes...like this:
string arithmenticProcess = txt_process.Text;
double firstNumber;
double.TryParse(txt_process.Text, out firstNumber);
double secondNumber;
double.TryParse(txt_process.Text, out secondNumber);
double answerNumber = 0;

double theResult = Calculator.GetEquals(arithmeticProcess, firstNumber, secondNumber, answerNumber);
txtDisplay.Text = Calculator.GetDisplayText();



In other words, you need to actually pass objects into the GetEquals method or else it wont work ;)

-Frinny

This post has been edited by Frinavale: 08 July 2010 - 10:24 AM

Was This Post Helpful? 0
  • +
  • -

#7 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:24 AM

Ok, I did get those two errors to go away, but the calc is not calulating now, If I input 9*9 it kicks back with an answer of 0.
why is it not calculating correctly?
private void btnEquals_Click(System.Object sender, System.EventArgs e)
        {
            Calculator.GetEquals(arithmeticProcess, firstNumber, secondNumber, 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
    {
        string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;
        
        public Calculator()
        {
        }

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

        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 GetEquals(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
                
            }
            if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
                
            }
            if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;
                
            }
            if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
            }
            return answerNumber;
        }

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


Was This Post Helpful? 0
  • +
  • -

#8 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:37 AM

In your Calculator class you have a private double answerNumber.
You your GetEquals method you are passing in a variable with the same name: answerNumber.

I don't know why you are passing in the variable answerNumber into the GetEquals method...there is no reason for it.

But what's essentially happening is that you are setting the "answerNumber" double that has a scope of the GetEquals method. What you should be doing is setting the private class member "answerNumber" instead.

When you have 2 variables with the same names, and you want to refer to the private class member you use the "this" keyword:
    public class Calculator
    {
        string arithmeticProcess;
        private double firstNumber;
        private double secondNumber;
        private double answerNumber;
        
     
        public static double Equals(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            
            if (arithmeticProcess == "+")
            {
                this.answerNumber = firstNumber + secondNumber;
            }
            if (arithmeticProcess == "-")
            {
                this.answerNumber = firstNumber - secondNumber;
            }
            if (arithmeticProcess == "*")
            {
                this.answerNumber = firstNumber * secondNumber;
            }
            if (arithmeticProcess == "/")
            {
                this.answerNumber = firstNumber / secondNumber;
            }
        }


Actually now that I've looked at your code a bit more...it appears that you are creating method level variables (firstNumber, secondNumber, arithmeticProcess, and answerNumber) but you should be using the class level variables (firstNumber, secondNumber, arithmeticProcess, and answerNumber).

Either set the class level variables to be the same as the values you are passing into the GetEquals method like this:
public static double Equals(string arithmeticProcess, double firstNumber, double secondNumber, double answerNumber)
        {
            this.arithmeticProcess= arithmeticProcess;
            this.firstNumber= firstNumber; 
            this.secondNumber= secondNumber; 
            this.answerNumber = answerNumber;

            if (this.arithmeticProcess == "+")
            {
                this.answerNumber = this.firstNumber + this.secondNumber;
            }
            if (this.arithmeticProcess == "-")
            {
                this.answerNumber = this.firstNumber - this.secondNumber;
            }
            if (this.arithmeticProcess == "*")
            {
                this.answerNumber = this.firstNumber * this.secondNumber;
            }
            if (this.arithmeticProcess == "/")
            {
                this.answerNumber = this.firstNumber / this.secondNumber;
            }
        }



Or better yet remove all of the parameters from the GetEquals method and set the class level variables using properties before calling the GetEquals method...

Changes to GetEquals method:
        public static double Equals()
        {
            
            if (arithmeticProcess == "+")
            {
                answerNumber = firstNumber + secondNumber;
            }
            if (arithmeticProcess == "-")
            {
                answerNumber = firstNumber - secondNumber;
            }
            if (arithmeticProcess == "*")
            {
                answerNumber = firstNumber * secondNumber;
            }
            if (arithmeticProcess == "/")
            {
                answerNumber = firstNumber / secondNumber;
            }
            return answerNumber;
        }


changes to button click method:
Calculator calc;
calc = new Calculator();
calc.FirstNumber = ....;
calc.SecondNumber = ....;
calc.Operation = ....;//<---this one you need to create

calc.Equals();
txtDisplay.Text = calc.GetDisplayText(answerNumber);


-Frinny

This post has been edited by Frinavale: 08 July 2010 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#9 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 174
  • View blog
  • Posts: 629
  • Joined: 03-February 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:44 AM

I think, part of the reason is because in your signature for GetEquals() you pass in a parameter called answerNumber. I think, this is a local variable inside the function. In other words, inside GetEquals() answerNumber is not the same as the class level variable answerNumber, which is still 0.

Sorry Frinavale, should have refreshed first :)

This post has been edited by demausdauth: 08 July 2010 - 10:45 AM

Was This Post Helpful? 0
  • +
  • -

#10 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 10:56 AM

can you give me an example....
I don't want to solve for me, but I learn more visually
Was This Post Helpful? 0
  • +
  • -

#11 Frinavale  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 203
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 11:14 AM

Demausdauth said the exact same thing I did...and I gave examples of how to fix this....

-Frinny
Was This Post Helpful? 0
  • +
  • -

#12 tricket_7  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 104
  • Joined: 09-May 09

Re: Basic Calculator in C#2008 Calculator Class question

Posted 08 July 2010 - 11:16 AM

Thank You guys for your help.... :smile2: :smile2:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1