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.
 
Federico Caselli said:
I'm using visual basic.net, I need to take in input a string containing an
expression and return the value, example:

This is possible by generating code, compile it and execute, all at runtime!

I've done it, works like a charm! This article got me started:

http://www.odetocode.com/Code/80.aspx

Steven

- - -
 
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top