Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Evaluate a string expression
Message
From
18/09/2006 16:48:41
Keith Payne
Technical Marketing Solutions
Florida, United States
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Miscellaneous
Thread ID:
01154848
Message ID:
01154915
Views:
16
>Hi,
>
>I would like to know if there a way in VB.Net 2005 to evaluate as string expression.
>
>Ex :
>
>
>Dim Test as String
>Dim Result as integer
>
>Test = "2 + 5"
>
>Result = EvaluateFonction(Test)
>
>
>Ex 2 :
>
>Dim Test as String
>Dim Result as integer
>dim iNumber as integer
>
>iNumber = 3
>
>Test = "iNumber * iNumber"
>
>Result = EvaluateFonction(Test)
>
>
>The thing I want to know is : it is exists a "EvaluateFonction" to evaluate any king of operation, not only mathematical one.
>
>thx

EDIT: Now I see the 2005 reference. This may not apply to 2005.

It is a lot of work to evaluate a run-time expression.

If the expression that you want to evaluate involves objects instead of literal expressions use PropertyInfo.GetValue or MethodInfo.Invoke to get the scalar value of the object's properties or the scalar return values of the methods. Then convert the scalar values to strings. And finally use something like the following kludge to evaluate the scalar expression:
        Dim dt As New System.Data.DataTable
        Dim c As New System.Data.DataColumn
        Dim c1 As New System.Data.DataColumn

        c.Expression = "'Hello' + ' there'"
        dt.Columns.Add(c)

        c1.Expression = "3 + 5"
        dt.Columns.Add(c1)

        Dim dr As DataRow = dt.NewRow
        dt.Rows.Add(dr)

        Console.WriteLine(dt.Rows(0)(0))
        Console.WriteLine(dt.Rows(0)(1))
        Console.ReadLine()
If this is part of a web forms application, it is a bit easier to use DataBinder.Eval to evaluate an expression instead of creating a table, columns, and rows.
Previous
Reply
Map
View

Click here to load this message in the networking platform