Let a user write an expression as a string and return a value

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using visual basic.net, I need to take in input a string containing an
expression and return the value, example:

"14=14" must return true
"14+14" must return 28
"8<4" must return false
 
Hi Federico,

I believe you will have to do this manually the hard way by splitting
operators and numbers by presedence and possibly recursively.
 
Thank you. The class Evaluator is really what I need. Just one thing: I need
to evaluate a boolean expression, like "84 < 70" or "7 >=8". So I tried to
add the following method to the class Evaluator
-------------------------------------------------------------------------------------
public static bool EvalToBoolean(string statement)
{
string s = EvalToString(statement);
return bool.Parse(s.ToString());

-------------------------------------------------------------------------------------

When I try to use the method

blnRes = Evaluator.EvalToBoolean("7<8")

I get an exception.

Is there a way to make it work?

Thank you
 
Back
Top