|
|
|
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: |
Click here to copy the following block | Function EvalExpression(ByVal expr As String) As Double Dim dt As New DataTable() dt.Columns.Add("Expr", GetType(Double), expr) dt.Rows.Add(dt.NewRow) Return CDbl(dt.Rows(0).Item("Expr")) End Function |
Here's an example that uses the above function: |
You can also support variables by creating one or more column named after the variables, as in this function: |
Click here to copy the following block | Function EvalExpression(ByVal expr As String, ByVal x As Double) As Double Dim dt As New DataTable() dt.Columns.Add("x", GetType(Double)) dt.Columns.Add("Expr", GetType(Double), expr)
dt.Rows.Add(dt.NewRow) dt.Rows(0).Item("x") = x Return CDbl(dt.Rows(0).Item("Expr")) End Function |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|