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

Failed to evaluate variables #49

Closed
trrahul opened this issue Mar 4, 2020 · 6 comments · Fixed by #50
Closed

Failed to evaluate variables #49

trrahul opened this issue Mar 4, 2020 · 6 comments · Fixed by #50

Comments

@trrahul
Copy link

trrahul commented Mar 4, 2020

I used the following code.

ExpressionEvaluator evaluator = new ExpressionEvaluator();
            evaluator.Variables = new Dictionary<string, object>();
            evaluator.Variables["a"] = 1;
            evaluator.Variables["b"] = 2;
            evaluator.Variables["c"] = "a+b";
            evaluator.Variables["d"] = "c+3";
            Console.WriteLine(evaluator.Evaluate("a"));
            Console.WriteLine(evaluator.Evaluate("b"));
            Console.WriteLine(evaluator.Evaluate("c"));
            Console.WriteLine(evaluator.Evaluate("d"));

The output was

1
2
a+b
c+3

Shouldn't the output be the following?

1
2
3
6
@codingseb
Copy link
Owner

codingseb commented Mar 4, 2020

Hello @trrahul.
No the output is right.

This because a variable contains a C# object and in your case. variables c and d contains a string. So they are interpreted as string.
So if you do this :

evaluator.Evaluate("c+d");
// or if
evaluator.Variables["h"] = "Hello";
evaluator.Variables["w"] = "World";
evaluator.Evaluate("h+\" \" +w");

you get :

a+bc+3
Hello World

but if you want to evaluate a string in a evaluation you could do something like this :

evaluator.Evaluate("Evaluate(c)");
evaluator.Evaluate("Evaluate(d)");

And then you will get :

3
6

See Custom variables and Functions for more infos

@codingseb
Copy link
Owner

codingseb commented Mar 5, 2020

After a little thought. I think it might be good to introduce a type of variable that could be evaluated when encountered to better answer to this issue.
Something like :

evaluator.Variables["c"] = new SubExpression("a+b");
evaluator.Variables["d"] = new SubExpression("c+3");

I Reopen this issue and will work on it for the next version of ExpressionEvaluator.
So @trrahul thanks for this issue.

@trrahul
Copy link
Author

trrahul commented Mar 6, 2020

Isn't it better to search for the variables in the expression and evaluate them in order instead of creating a new subexpression?

Because in this case, please correct me if I am wrong, every variable has to be created as a subexpression if they are used later in another expression.

@codingseb
Copy link
Owner

codingseb commented Mar 6, 2020

You need to understand that variables are just containers like C# variables or any programming language variables. It could contains anything (string, int, float, Regex, File or a custom class...) and a string is not a expression it's a string and it's right like this because you can do operation with string variables so you need a specific type to specify that the content of a variable must be evaluate otherwise the variable is simply evaluated as it's content.

Because in this case, please correct me if I am wrong, every variable has to be created as a subexpression if they are used later in another expression.

No if a variable is just a value like your variables a or b (int) they are interpreted as this.
So only variables that need to be evaluated when met need to be contains in a specific type that ExpressionEvaluator know it' need to be sub evaluate.

But of course if you also encapsulate a and b with subexpression it will also work. Because the subevaluation will return their values.

@trrahul
Copy link
Author

trrahul commented Mar 6, 2020

Thanks for the clarification. I was trying out different expression evaluators and this issue happened with all the libs I tried except lambdaparser. It was able to evaluate the expression correctly, but it had another problem ExpressionEvaluator did not have. nreco/lambdaparser#22

@codingseb codingseb linked a pull request Mar 6, 2020 that will close this issue
Merged
@codingseb
Copy link
Owner

I just published version 1.4.9.0 that allow to the use of SubExpression here is the doc on the wiki SubExpression variable

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

Successfully merging a pull request may close this issue.

2 participants