NCalc missing EOF

2.4k Views Asked by At

i was trying to evaluate expression using NCalc,

Expression ex = new Expression("3[X] + 4[Y]");
ex.Parameters["X"] = 10;
ex.Parameters["Y"] = 20;

I have been getting this error:

missing EOF at '[x]' at line 1:1

Not sure why I have getting this error? Does the square brackets for variables have something to do with this?

1

There are 1 best solutions below

2
Dustin Kingen On

See NCalc's parameters documentation.

Using static parameters:

Expression e = new Expression("2 * [x] ^ 2 + 5 * [y]");
e.Parameters["x"] = 5;
e.Parameters["y"] = 1;

Console.WriteLine(e.Evaluate());

From your example:

Expression ex = new Expression("3 * [X] + 4 * [Y]");
ex.Parameters["X"] = 10;
ex.Parameters["Y"] = 20;

// 110
Console.WriteLine(ex.Evaluate());