A useful feature in Visual Basic 6 and Office/VBA is the Eval() function. Eval evaluates the supplied string and return a result. This offers the ability to evaluate arbitrary statements at runtime. Unfortunately, neither C# nor Visual Basic .NET has an equivalent feature.
Fortunately, .NET can call components authored in other .NET languages to make this fairly simple to implement.
Our first piece of the puzzle is JScript.NET, the .NET version of JavaScript. JScript.NET has an Eval function. The question is how to get to that function from our C# or Visual Basic .NET projects. To build your own Eval equivalent, follow these steps.
class JScriptEval { function Evaluate(evalString : String) { return eval(evalString); } }
C:\WINDOWS\Microsoft.NET\Framework\ver\jsc.exe /t:library jScriptEval.jswhere "ver" is your .NET Framework version to the directory where the compiler is installed.
Test your EVAL library like this:
Dim j As New JScriptEval Dim s1 As String = "1 + 2 * 3" MsgBox(j.Evaluate(s1))
You should be aware that using Eval statements can lead to potential security risks. Eval can execute just about any program code. There are two ways to minimize this. First, you can use a regular expression evaluator to make sure that the string passed to Eval contains only letters, operators and numbers, not words. Secondly, consider using .NET security features to disallow any potentially dangerous operations before code is executed by Eval.
Thank you! Thank you! I just finished reading this document, which was part of a link in the recent Buzz newsletter. I have printed it for others to read, especially those skeptical on the powers of Access and its capabilities.
Darren D.
All Our Microsoft Access Products