Showing posts with label String expression. Show all posts
Showing posts with label String expression. Show all posts

Sunday, May 23, 2010

Execute a string expression in X++

Sometimes we need to execute a string expression in X++. This may be the case when we need to take the full expression as input from the user and then to execute it. To do this we need to create a method which basically returns the string expression. Then pass this method runBuf() for execution as shown,

static void executeExpression(Args _args)
{
str expr = '2 * (16/8)';
str method = @'real calculate(){return ' + expr + ';}';
real result;
;
result = runBuf(method);
info (strfmt("Calculation result is %1", result));
}
This job will display an infolog of

Calculation result is 4