Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//read and convert first operand 
            operand1 = Convert.ToDouble(operand1TextBox.Text);
            //read and convert second operand
            operand2 = Convert.ToDouble(operand2TextBox.Text);
            //compute result
            answer = operand1 + operand2;
            //format result as string and display on form
            answerTextBox.Text = String.Format("{0:N3}", answer);
        }

        private void subtractButton_Click(object sender, EventArgs e)
        {
            //read and convert first operand 
            operand1 = Convert.ToDouble(operand1TextBox.Text);
            //read and convert second operand
            operand2 = Convert.ToDouble(operand2TextBox.Text);
            //compute result
            answer = operand1 - operand2;
            //format result as string and display on form
            answerTextBox.Text = String.Format("{0:N3}", answer);
        }

        private void multiplyButton_Click(object sender, EventArgs e)
        {
            //read and convert first operand 
            operand1 = Convert.ToDouble(operand1TextBox.Text);
            //read and convert second operand
            operand2 = Convert.ToDouble(operand2TextBox.Text);
            //compute result
            answer = operand1 * operand2;
            //format result as string and display on form
            answerTextBox.Text = String.Format("{0:N3}", answer);
        }

        private void divideButton_Click(object sender, EventArgs e)
        {

            //read and convert first operand 
            operand1 = Convert.ToDouble(operand1TextBox.Text);
            //read and convert second operand
            operand2 = Convert.ToDouble(operand2TextBox.Text);
            //compute result
            if (operand2 != 0)
            {
                answer = operand1 / operand2;
                //format result as string and display on form
                answerTextBox.Text = String.Format("{0:N3}", answer);
            }
            else
            {
                //display the error
                MessageBox.Show("Cannot divide by zero. Re-enter the denominator.", "Error in denominator", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //set focus back to the textbox with the data entry error
                operand2TextBox.Focus();
                operand2TextBox.SelectAll();
            }
        }

        private void clearButton_Click(object sender, EventArgs e)
        {
            //Clear text Boxes
            operand1TextBox.Clear();
            operand2TextBox.Clear();
            answerTextBox.Clear();
            //reset the focus to the top TextBox
            operand1TextBox.Focus();
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}


What I have tried:

I honestly don't know what to do.
Posted
Updated 4-Nov-20 20:12pm
Comments
Patrice T 4-Nov-20 18:20pm    
And you are too faint to show us the exact error messages ?
Dave Kreskowiak 4-Nov-20 19:35pm    
You're going to have to supply the error messages to get any kind of usable answer.

CS0029 means "Cannot implicitly convert type 'type' to 'type'"

But, we have no idea what lines it's throwing on nor what types and variables are involved.

See example here: c# - Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'string' - Stack Overflow[^]

So check your variable declarations, e.g. check if operand1 is declared as double:
Public double operand1;

operand1 = Convert.ToDouble(operand1TextBox.Text);

Also see: C# | Data Types - GeeksforGeeks[^]
 
Share this answer
 
In addition to Rick's comments, I'll add a few things.
Firstly, don't use Convert for user input - everybody makes mistakes, and if you app crashes as a result, it's rather rude! Use the TryParse methods instead as they return a "OK / Failed" result you can work with.

Secondly, there is a lot of duplicated code there: your add, subtract and multiply methods differ only in one character!
Instead of having separate methods to handle the operators, use a single one which d3ecides what to based on the button text:
C#
private void OperatorButton_Click(object sender, EventArgs e)
    {
    double op1;
    if (!double.TryParse(operand1TextBox.Text, out op1))
        {
        MessageBox.Show($"\"{operand1TextBox.Text}\" is not a number");
        return;
        }
    double op2;
    if (!double.TryParse(operand2TextBox.Text, out op2))
        {
        MessageBox.Show($"\"{operand2TextBox.Text}\" is not a number");
        return;
        }

    double result;
    if (sender is Button b)
        {
        switch (b.Text)
            {
            default: MessageBox.Show($"Unknown button pressed: \"{b.Text}\""); return;
            case "+": result = op1 + op2; break;
            case "-": result = op1 - op2; break;
            case "*": result = op1 * op2; break;
            case "/":
                if (op2 == 0.0)
                    {
                    MessageBox.Show("Divide by zero is not possible");
                    operand2TextBox.Focus();
                    operand2TextBox.SelectAll();
                    return;
                    }
                result = op1 / op2;
                break;
            }
        answerTextBox.Text = String.Format("{0:N3}", result);
        }
    }
That way, it's easier to keep all your code up-to-date with the latest changes as it only need to be altered in one place.

You will also note that I haven't added any comments to that code: that's because it doesn't actually need any - all of your comments are just describing what the code is doing using slightly different words to the code itself:
//read and convert first operand
operand1 = Convert.ToDouble(operand1TextBox.Text);
//read and convert second operand
operand2 = Convert.ToDouble(operand2TextBox.Text);
//compute result
answer = operand1 - operand2;
//format result as string and display on form
answerTextBox.Text = String.Format("{0:N3}", answer);
None of those comments add anything to comprension of the code - just just make it harder to see the code itself! Comments should describe why the code is doing something, not what the code is doing - they add information, instead of duplicating it. If you use the right naming conventions and avoid "clever code" it should all be self-documenting, and that means that the comments aren't out of date - when you change code, it's too easy to forget to update "descriptive comments" like yours and then they no longer reflect the code at all and are worse than useless!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900