-2

Is there a way a comparison in a string value can return a Boolean value. Example. If (5 > 5000) would obviously return a false value. But what i wanted to do is have the "5 > 5000" return a false value.

Example.

string com = "5 > 10";

so is there a way to make this com variable return a false value as if it was a comparison between integers.

GSerg
  • 76,472
  • 17
  • 159
  • 346
EngAbth9
  • 31
  • 9
  • 6
    http://stackoverflow.com/questions/12118077/using-javascript-for-custom-purposes/12118208#12118208 – L.B Nov 06 '12 at 17:49
  • 5
    You should be really sure that you want to do something like this before implementing it. Running strings as code is potentially a *major* security hole if it's based on user provided data. (And if it's not, you probably don't need to do it in the first place.) – Servy Nov 06 '12 at 17:51
  • You can also try NCalc: http://ncalc.codeplex.com/ – tukaef Nov 06 '12 at 17:58

6 Answers6

5

No built-in way but NCalc can help here

NCalc.Expression expr = new NCalc.Expression("5>10");
bool b = (bool)expr.Evaluate();

You can even use parameters

NCalc.Expression expr = new NCalc.Expression("a<b");

expr.EvaluateParameter += (name, args) =>
  {
      if (name == "a") args.Result = 5;
      if (name == "b") args.Result = 10;
  };
bool b = (bool)expr.Evaluate();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • @Enq If you've found some answers useful see this link: [How does accepting an answer work?](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – L.B Nov 07 '12 at 10:59
3

There is no built-in way to do this.

Although there are a couple of ways to approach this, one is to simply parse the text yourself. I did this in the code presented in the article A C# Expression Evaluator. You might want to review that code.

Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
  • In addition to the article you posted, there are libraries such as FLEE (http://flee.codeplex.com/) and NCalc (http://ncalc.codeplex.com/) which do pretty much the same thing but have additional features. Possibly overkill for Enq's usage, but worthwhile to consider for anyone reading this question/answer. – Chris Sinclair Nov 06 '12 at 18:00
3

No, this can't be done directly. You should write your own class or extend the String class. For handling a string such as "5 < 10", you need your own method.

You should search the string for signs that indicate comparison, such as "<", "==" etc, then split it and perform the comparison.

Basically: doing it yourself is the only way, but you can try to do it in an elegant way.

2

Short answer: no.

Long answer: feel free to parse the string yourself, looking for > < and =. Split by whitespace, parse ints then evaluate. It might get harder if you want it to work with parentheses as well...

Austin Salonen
  • 49,173
  • 15
  • 109
  • 139
TDaver
  • 7,164
  • 5
  • 47
  • 94
2

Not directly, per se (short of the unsafe Javascript eval-execute-my-data hack) but you can try parsing it yourself, depending on how complicated of an expression you want to accept. For example, this should work with the string you have:

var arr = com.Split('>').Select(x=>int.Parse(x.Trim())).ToArray();
return arr[0] > arr[1];

You can also use regular expressions to get more complicated (untested, but it ought to work):

var r = new Regex(@"(\d+)\b*(<|>|=|<=|>=)\b*(\d+)")
var match = r.Match(com);
if(match.Success)
{
    var a = int.Parse(match.Captures[0]);
    var b = int.Parse(match.Captures[2]);

    switch(match.Captures[1])
    {
         case "<":
             return a < b;
         case "=":
             return a = b;
         case ">":
             return a > b;
         case "<=":
             return a <= b;
         case "<=":
             return a >= b;
    }
}

//uh-oh
throw new ArgumentException("com");
lc.
  • 113,939
  • 20
  • 158
  • 187
1

Consider using FLEE:

Flee is an expression parser and evaluator for the .NET framework. It allows you to compute the value of string expressions such as sqrt(a^2 + b^2) at runtime. It uses a custom compiler, strongly-typed expression language, and lightweight codegen to compile expressions directly to IL. This means that expression evaluation is extremely fast and efficient. Try out the demo, which lets you generate images based on expressions, and see for yourself.

With FLEE you can easily accomplish this using something like:

        var con = new ExpressionContext();
        const string com = @"5 > 5000";
        var comparison = con.CompileDynamic(com);
        var result = comparison.Evaluate();
        MessageBox.Show(result.ToString());

HTH...

ssrhhrm
  • 33
  • 7