Evaluate an expression at runtime

The .NET framework doesn’t offer any direct way to evaluate an expression that has been entered by the end user when the application is running. However, it is quite simple to create a simple expression evaluator based on calculated columns in DataTable. The following routine does the trick:

Function EvalExpression(ByVal expr As String) As Double    ' create a new DataTable containing a calculated column    Dim dt As New DataTable()    dt.Columns.Add("Expr", GetType(Double), expr)    ' add a dummy row    dt.Rows.Add(dt.NewRow)    ' return the value of the calculated column    Return CDbl(dt.Rows(0).Item("Expr"))End Function

Here’s an example that uses the above function:

Dim expr As String = "100 * (2 + 3)"Console.WriteLine(EvalExpression(expr))   ' displays 500

You can also support variables by creating one or more column named after the variables, as in this function:

Function EvalExpression(ByVal expr As String, ByVal x As Double) As Double    ' create a new DataTable     Dim dt As New DataTable()    ' first, add a column named after the variable    dt.Columns.Add("x", GetType(Double))    ' then add the calculated column    dt.Columns.Add("Expr", GetType(Double), expr)    ' add a dummy row    dt.Rows.Add(dt.NewRow)    ' set the value of the variable    dt.Rows(0).Item("x") = x    ' return the value of the calculated column    Return CDbl(dt.Rows(0).Item("Expr"))End Function

You can use the overloaded version of the function as follows:

Dim expr As String = "x*x + 3*x + 4"Dim res As Double = EvalExpression(expr, 10)

Notice that the operation of creating the DataTable and its dummy row is rather time-consuming. If you plan to reuse the same expression with different values for different values of the variable you should create the DataTable only once and reuse it for all subsequent evaluations.

The expression parser built in the DataSet and DataTable objects supports all the usual math and comparison operators, except the ^ operator. You can use the % symbol in place of the MOD operator, and the + symbol for string concatenations. The LIKE operator is similar to the T-SQL and VB6 operator with same name. A few functions are supported as well: IIF, LEN, ISNULL, CONVERT, and SUBSTRING. For more information see the SDK documentation.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may