Question: The assignment is to create a simple caluclator in C# using Visual Studio. I have attached my code below however I keep receiving errors that

The assignment is to create a simple caluclator in C# using Visual Studio. I have attached my code below however I keep receiving errors that " the name 'result' does not exist in the current context" for like every variable, method, etc. I would appreciate any help you can give me. Thanks!

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

namespace WindowsCalculator { public partial class Form1 : Form { decimal value1; decimal value2; decimal result; string operation; int count; bool div = false; decimal input;

private bool TestValue(String numTest) { bool test = decimal.TryParse(numTest, out result); if (test == false) { MessageBox.Show("Invalid input"); } textBox1.Clear(); textBox1.Focus(); return test; } public Form1() { InitializeComponent(); }

private void add_Click(object sender, EventArgs e) // ADD button { div = false; if (TestValue(textBox1.Text)) { value1 = input; count = 2; } }

private void subtract_Click(object sender, EventArgs e) // SUBTRACT button { div = false; if (textBox1.Text != "") { if (TestValue(textBox1.Text)) { value1 = input; } count = 1; } }

private void multiply_Click(object sender, EventArgs e) // MULTIPLY button { div = false; if (TestValue(textBox1.Text)) { value1 = input; count = 3; } }

private void divide_Click(object sender, EventArgs e) // DIVIDE button { div = false; if (TestValue(textBox1.Text)) { value1 = input; count = 4; } }

private void equals_Click(object sender, EventArgs e) // EQUALS button { div = false; compute(count); clear_Click.Enabled = true; } }

private void clear_Click(object sender, EventArgs e) // CLEAR button { div = false; TextBox1.Clear(); TextBox1.Focus(); count = 0; }

private void textBox1_TextChanged(object sender, EventArgs e) {

} public void compute (int count) { switch (count) { case 1: result = value1 + float.Parse(TextBox1.Text); TextBox1.Text = result.ToString(); break; case 2: result = value1 - float.Parse(TextBox1.Text); TextBox1.Text = result.ToString(); break; case 3: result = value1 * float.Parse(TextBox1.Text); TextBox1.Text = result.ToString(); break; case 4: if (float.Parse(TextBox1.Text) == 0) { MessageBox.Show("Sorry! This number cannot be divided by 0."); } else { result = value1 / float.Parse(TextBox1.Text); } TextBox1.Text = result.ToString(); break;

default: break; } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!