picture a:
picture b:
So I found this functionality can also be used trough x++. The result was a static method which can evaluate an expression to be calculated.
That's the code behind:
///And that's how we use it:; /// Calculates a simple math task /// ; /// ; /// A simple mathematic expression. /// +-/* operators and brackets can be used /// ////// The calculated result. If the expression /// does not match the expected format, /// zero is returned. /// ; ///; /// Uses functionality from /// ; static client real calcExpression(str _expression) { SysFormRun formRun; Args args = new Args(); FormBuildControl buildCtrl; FormRealControl realCtrl; ; args.name(formstr(Dialog)); formRun = classFactory.formRunClass(args); buildCtrl = formRun.form().design() .addControl(FormControlType::Real, classstr(FormRealControl)); formRun.init(); realCtrl = formRun.design() .control(buildCtrl.id()); realCtrl.pasteText(_expression); return realCtrl.realValue(); }FormRealControl , /// therefore client execution /// is required. ///
info(strfmt('%1', MyClass::calcExpression('5+(5*2)')));
Keep in mind, this function inherits all limits which the FormRealControl calculation feature owns.
You can also use the function runBuf() which supports full X++ syntax.
ReplyDeletehttp://msdn.microsoft.com/en-us/library/aa656300.aspx
/Bjørn
Of course the runBuf function can evaluate a calculation too, but say the user can define the calculation expression in a text field; we won't give them access to the entire X++ functionality, won't we?
ReplyDeleteWhat I mean is, you don't need any validation of the expression when use the RealFormControl's feature.