Alex Rivera | Logout

operators as strings

Asked 2008-10-06T15:04:25.287
20

I need to evaluate a mathmatical expression that is presented to me as a string in C#. Example noddy but gets the point across that the string as the expression.

I need the evaluate to then populate an int.

There is no Eval() in C# like in others langugaes...

String myString = "3*4";

Edit:

I am on VS2008

Tried the Microsoft.JScript. = Its deprecated method (but still complies - warning)

However the Microsoft.JScript dll that I have doens work on

public object InvokeMember(string name, BindingFlags invokeAttr, Binder binder, object target, object[] args);

Complains that there is a missing ";" go figure...

EDIT 2

Solution - was the codeDom one - it worked for as there are no security issue - only me ever going to be running the code. Many thanks for the replies ...

And the link to the new Dragon Book awesome

EDIT 3

Matt dataTable.Compute() also works - even better for the security conscious. (parameter checking noted)

c#
Edit
Report

1 Answer

0

Some other suggestions:

  • Mono 2.0 (came out today) has an eval method.
  • You can easily write a small domain specific in boo.
  • You can create an old school recursive-descent EBNF parser.
answered 2008-10-06T19:33:24.557

Your Answer