Search code examples
axaptadynamics-ax-2012boolean-expressionboolean-operations

How do I evaluate a boolean expression given as a String in X++ [AX 2012]


I have a scenario, in which I want to evaluate a boolean expression given as a string.

For example, "1==4" when passed to some function would evaluate-to/return FALSE.

Similarly, "1==1" when passed to the same function would evaluate-to TRUE.

If this can be done, then I would definitely want some way in which I can evaluate a dynamic boolean expression. For example, "workHours==4" is the the string expression and workHours is a variable.

Let me know of any solution if you know. Thanks.


Solution

  • Self learned the answer:

    Here's the code sample, how you can do it.

    static void Job22(Args _args)
    {
    
     str expr = 'ab==2';
     str method = @'boolean eval(int ab){return ' + expr + ';}';
     boolean result;
     ;
     result = runBuf(method, 5);
     info (strfmt("Calculation result is %1", result));
    
    }