0
 string stringValue = "1+2+3";

How can I convert the above string value with operators, "1+2+3", so that it can be assigned to the intvalue as shown below in C#.

 int intValue = 1+2+3;

i.e intValue = 6;

6
  • See: stackoverflow.com/questions/333737/… Commented May 31, 2013 at 15:58
  • 2
    You could compile it with Microsoft.CSharp.CSharpCodeProvider of course... Commented May 31, 2013 at 15:58
  • 1
    Lol that's actually the answer there. My god, what happened to good old stack based processing of a simple mathematical expression? Commented May 31, 2013 at 15:59
  • 1
    What mathematical operators might the string contain? Commented May 31, 2013 at 16:03
  • Please try harder to research instead of asking these redundant questions. It's easier to google than to type all those stuff you've typed. Commented May 31, 2013 at 16:07

3 Answers 3

0

You need to compile on the fly. See for example Evaluate C# Code or Flee.

Sign up to request clarification or add additional context in comments.

Comments

0

Use DataTable.Compute (http://msdn.microsoft.com/en-us/library/system.data.datatable.compute.aspx)

  var result = new DataTable().Compute("2-3/4*12", null);

ANd this has been asked so many times before.

Comments

0

I have used this library in many projects: C# calculation engine

It's very simple to use:

var ce = new CalcEngine();
double result = (double)ce.Evaluate("1+2+3");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.