EJohn

I can't get the "result" to print out the answer.

Can you see why please

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Calculator

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void calculateButton_Click(object sender, EventArgs e)

{

double value1 = Double.Parse(value1Box.Text);

double value2 = Double.Parse(value2Box.Text);

double result = 0.0;

Calculator calc = new Calculator();

switch (operation.Text)

{

case "Add":

result = calc.Add(value1, value2);

break;

case "Subtract":

result = calc.Subtract(value1, value2);

break;

case "Multiply":

result = calc.Multiply(value1, value2);

break;

case "Divide":

result = calc.Divide(value1, value2);

break;

}

}

private void operation_SelectedIndexChanged(object sender, EventArgs e)

{

}

private void value2Box_TextChanged(object sender, EventArgs e)

{

}

}

}



Re: Visual C# General Windows c code help please, thank you!

Jay Vora

its a prob when u dnt cnvert operation.text to string..

try messagebox.show(operation.text) it needs to b cnverted ToString..

try ... switch(Operation.text.Tostring) ..u will get ur part.






Re: Visual C# General Windows c code help please, thank you!

Friendly Dog

First of all, your "result" variable is not assigned to any output.

Second, it's not a good idea to use string constants to define a set of operations, try to use enum such as

enum Operators

{

Add,

Substract,

.....

}






Re: Visual C# General Windows c code help please, thank you!

ahmedilyas

indeed, it is because you have only got "result" in the scope of 1 method, you should either pass that result to another method for it to show the result to the user or declare it globally. An example:




using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace Calculator
{
public partial class Form1 : Form

{
private int result = 0;
public Form1()
{
InitializeComponent();
}
private void calculateButton_Click(object sender, EventArgs e)
{
double value1 = Double.Parse(value1Box.Text);
double value2 = Double.Parse(value2Box.Text);
double result = 0.0;

Calculator calc = new Calculator();
switch (operation.Text)
{
case "Add":
result = calc.Add(value1, value2);
break;
case "Subtract":
result = calc.Subtract(value1, value2);
break;
case "Multiply":
result = calc.Multiply(value1, value2);
break;
case "Divide":
result = calc.Divide(value1, value2);
break;

}


}

//lets say we have a button that shows the answer when we clicked on it:
private void ButtonShowResult_Click(object sender, EventArgs e)
{
MessageBox.Show(this.result.ToString());
}