-3

Scenario:

  • the form consists of 2 text boxes and a button.
    • in textBox1 the user inputs: 4+7-3+10
    • the button is pressed
    • in textBox2 the output will work out the sum = 18

Can this be achieved in c#? and if yes, how?

Thanks.

1 Answers1

1

Yes, it can be achieved with System.Data.DataTable.Compute:

var calculator = new DataTable();
int result = (int)calculator.Compute("4+7-3+10", null); // 18

Have a look at the remarks section of DataColumn.Expression to see what is supported.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939