-5

Possible Duplicate:
calculate result from a string expression dynamically

I have the following formula saved in XML as element value- "d1*2".
After I extract this from the XML as a string how can I assign a value to the "d1"
and calculate the formula result?

Community
  • 1
  • 1
Avi.S.
  • 163
  • 2
  • 3
  • 12
  • 2
    See http://stackoverflow.com/questions/10062875/c-user-defined-formula or http://stackoverflow.com/questions/12431286/calculate-result-from-a-string-expression-dynamically – L.B Oct 13 '12 at 23:28

1 Answers1

1

This looks like it's exactly what you're looking for:

https://github.com/Giorgi/Math-Expression-Evaluator

The author's done a really good and thorough write-up of the usage and design here:

http://www.aboutmycode.com/net-framework/building-expression-evaluator-with-expression-trees-in-csharp-table-of-contents/

Usage goes like this (copied from the examples):

var a = 6;
var b = 4.32m;
var c = 24.15m;
var result = engine.Evaluate("(((9-a/2)*2-b)/2-a-1)/(2+c/(2+4))", new { a, b, c});

Google is, of course, your friend... there are a bunch of others out there, but this by far looks like the most mature project.

EDIT: This other SO answer also mentions NCalc, which is similar: https://stackoverflow.com/a/10063089/381587

Community
  • 1
  • 1
Richard K.
  • 2,034
  • 1
  • 14
  • 15