Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
I have this error when I try to run the code.
C#
public partial class Calculator : Form
 {

    public Calculator()

    {
       InitializeComponent();

    }


    private void button1_Click(object sender, EventArgs e)
    {
       double a, b, r;
      string result = textBox4.Text.ToString();
       string operand = textBox2.Text.ToString();
       {

         switch ( result )
         {


          case " + " :
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a+b;
             textBox4.Text = result.ToString();
             break;

          case " - ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a-b;
             textBox4.Text = result.ToString();
             break;

           case " * ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a*b;
             textBox4.Text = result.ToString();
             break;

          case " / ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a/b;
             textBox4.Text = result.ToString();
             break;

          default:
             result = "This is not an operand";
             break;

         }

       }

    }
 }


TextBox1=first number
TextBox3=second number
TextBox2=operand
TextBox4=result
button1=result
Any help would be greatly appreciated.

Thank you.
Posted
Updated 18-Sep-13 18:06pm
v2
Comments
The Doer 19-Sep-13 0:03am    
Where is the error?
The Doer 19-Sep-13 0:04am    
string operand = textBox2.Text.ToString();
{
Why you put braces there when you don't have any condition ?
Thanks7872 19-Sep-13 0:07am    
Where is the error? Whats the error?
SoMad 19-Sep-13 0:16am    
For one, you are doing switch( result ), when you should be doing switch( operand ).

Soren Madsen
Member 10274631 20-Sep-13 0:35am    
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
double a, b, r; // to represent textboxes
string result = textBox4.Text.ToString(); // to represent the result
string Operation = textBox2.Text.ToString(); // to represent the operation

{
switch (Operation)
{
case "+":
a = double.Parse(textBox1.Text); // input string incorrect format
b = double.Parse(textBox3.Text); // if I leave blank
r = a + b;
textBox4.Text = r.ToString();
break;

case "-":
a = double.Parse(textBox1.Text);
b = double.Parse(textBox3.Text);
r = a - b;
textBox4.Text = r.ToString();
break;

case "*":
a = double.Parse(textBox1.Text);
b = double.Parse(textBox3.Text);
r = a * b;
textBox4.Text = r.ToString();
break;

case "/":
a = double.Parse(textBox1.Text);
b = double.Parse(textBox3.Text);
r = a / b;
textBox4.Text = r.ToString();
break;

default: // if the user input a value not in the operation.
textBox4.Text = "Cannot compute";
break;
}
if (String.IsNullOrEmpty(textBox1.Text)) // if user input nothing in textBox2
{
label6.Text = "Please enter the first number"; // label7 should appear this text.
}
if (String.IsNullOrEmpty(textBox2.Text)) // if user input nothing in textBox2
{
label7.Text = "Please enter an operator"; // label7 should appear this text.
}
if (String.IsNullOrEmpty(textBox3.Text)) // if user input nothing in textBox2
{
label8.Text = "Please enter the secong number"; // label7 should appear this text.
}
{
}
}
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = textBox1.Text.ToString();
}

private void textBox2_TextChanged(object sender, EventArgs e)
{


}

private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = textBox3.Text.ToString();
}

private void textBox4_TextChanged(object sender, EventArgs e)
{
textBox4.Text = textBox4.Text.ToString();
}

private void label6_Click(object sender, EventArgs e)
{

}

}
}



a = double.Parse(textBox1.Text); // if I leave blank textbox1 incorrect input
b = double.Parse(textBox3.Text); // same in textbox3 leave blank incorrect input.

What I did wrong here?

1 solution

NOTE: operand menas the digits you are multiplying not the operation youa re performing on them. So i changed operand to operation

C#
public partial class Calculator : Form
 {
 
    public Calculator()
 
    {
       InitializeComponent();
 
    }
 

    private void button1_Click(object sender, EventArgs e)
    {
       double a, b, r;
     // string result = textBox4.Text.ToString();
       string Operation = textBox2.Text.ToString();
       {
 
         switch ( Operation )
         {
 

          case " + " :
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a+b;
             //textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          case " - ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a-b;
            // textBox4.Text = result.ToString();
            textBox4.Text = r.ToString();
             break;
 
           case " * ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a*b;
            // textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          case " / ":
             a = double.Parse(textBox1.Text);
             b = double.Parse(textBox3.Text);
             r = a/b;
             //textBox4.Text = result.ToString();
             textBox4.Text = r.ToString();
             break;
 
          default:
             result = "This is not an operand";
             break;
 
         }
 
       }
 
    }
 }


I have not tested the code but its as simple as clicking on the button will generate your result on textbox4 on the basis of TextBox1 (operand),textBox3(operand) and textbox2(operation).
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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