1

I'm using NCalc to create Mathematical expression in C#:

       Expression e = new Expression("2 + 3 * 5");

        Debug.Assert(17 == e.Evaluate());

But second line gives me an error - "Operator == cannot be applied to operands of type int and object"

How to solve this problem?

user2262230
  • 199
  • 1
  • 3
  • 17
  • 8
    cast?.................. – Mitch Wheat Dec 23 '13 at 00:49
  • You need to cast the result as the value will need unboxing! e.g. `(int)e.Evaluate`, this is assuming that `e.Evaluate` does in fact result in an `int` and not some representative type or container type – Charleh Dec 23 '13 at 00:51
  • Isn't the error message explicit enough? The null exception one can be weird for beginners, but that one is really pretty straightforward... – Pierre-Luc Pineault Dec 23 '13 at 00:53

1 Answers1

2

The Evaluate() method returns an object (from the source code), so you need to insert a cast to make this work:

Debug.Assert(17 == (int) e.Evaluate());

The "simple expressions" example on the NCalc home page is incorrect.

adrianbanks
  • 81,306
  • 22
  • 176
  • 206
  • I tried casting but then I got an error - 'Cannot find type System.ApplicationException in module mscorlib.dll' – user2262230 Dec 23 '13 at 06:32