Quick expression parsing tip


In Windows, you can avoid "manual" parsing of arithmetic expressions by using the Microsoft Script Control COM object. Although it is a bit dangerous to run unsupervised scripts, it is a very handy quick way to parse any numerical expression string. It is recommended if the string is created/used in your program internally. It is NOT recommended if the "script string" is provided outside the program and you cannot control its content.

All you have to do is reference the COM library Microsoft Script Control 1.0. Then write the following under a click event -for example- to test the parsing immediately!

MSScriptControl.ScriptControl c = new MSScriptControl.ScriptControl();
c.Language = "VBScript";
double v = c.Eval("5+6*8");
MessageBox.Show(v.ToString());
You can use similar code in other languages that supports COM such as Visual Basic (including VBA).

Comments

Popular posts from this blog

Write Unicode text using VBA

Calling Fortran intrinsic functions from Visual Basic

Dictionary class extensions (CopyTo, Sort) (C#)