Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot evaluate formula #1

Closed
Rbn3D opened this issue Jan 21, 2017 · 2 comments
Closed

Cannot evaluate formula #1

Rbn3D opened this issue Jan 21, 2017 · 2 comments
Assignees
Labels

Comments

@Rbn3D
Copy link

Rbn3D commented Jan 21, 2017

Consider the following code:

public static InputValidation ValidCustomFormula = delegate (string val) {
            if (String.IsNullOrWhiteSpace(val))
                return "";
            else
            {

                var expr = new Expression(val);

                decimal x = 5m;
                decimal a = 6m;
                decimal b = 7m;

                expr.Parameters["x"] = x;
                expr.Parameters["a"] = a;
                expr.Parameters["b"] = b;

                try
                {
                    Func<float> f = expr.ToLambda<float>(); // Here it throws System.ArgumentNullException. Parameter name: expression

                    float res = f();
                }
                catch(Exception e)
                {
                    return "There was an error while parsing the custom formula: " + e.Message;
                }
          }
    }

    var res = ValidCustomFormula("2 + 2 - a - b - x");

As long as I don't include any parameter in the expression itselt (i.e.: "2 + 2 + 2") it works, but it doesn't when I include some parameter. Am I missing something? Looks like a bug.

Thanks.

sklose added a commit that referenced this issue Jan 21, 2017
@sklose sklose self-assigned this Jan 21, 2017
@sklose sklose added the bug label Jan 21, 2017
@sklose
Copy link
Owner

sklose commented Jan 21, 2017

Thanks for reporting this issue. The Parameters dictionary was not supported by the lambda expression compiler. I added support for that, but notice that values contained in the Parameters dictionary become constants in the lambda expression. If you need the ability to change those values after compilation you should look into using an ExpressionContext as described in README.md.

@sklose sklose closed this as completed Jan 21, 2017
@Rbn3D
Copy link
Author

Rbn3D commented Jan 21, 2017

Wow, thanks for the quick fix and response, I'll check it out 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants