Eval!

J

John

I need to use the eval function in my C# CF program. I will be having
a file full of expressions that each of which evaluates to a number.
So I just got to have eval. However C# does not have eval. The .NET
way is to write the module in JScript.NET and call it from C#. But CF
does not have JScript.NET. I can't get a handle on the reflection API
for Emit. Can anyone point me to code that shows this? The old usenet
links that pointed to it are dead. Any other ideas?

Thanks.
 
A

Alex Feinman [MVP]

I don't think you can do it on CF. There is no Reflection.Emit and no
JScript.NET. You will need to write your own parser and expression
evaluator... Not fun at all.
 
A

Alberto Silva

Sugestion: If you're using SQL Server CE, and the expression is not too
complicated, try to create a query with that expression... you could have a
table with several fields (A, B, C,...) and one formula field if you have
several expressions among the code that can be changed.

When you want to get a result, you fill the values on the several needed
fields of a specific record, then you get the formula from it's field and
then when you create the command, you use something like:

formula = "(A+B)^2"
command = "SELECT " & formula & " AS RESULT FROM mytable"

I hope it works...

Alberto Silva
 
J

John

Alberto Silva said:
Sugestion: If you're using SQL Server CE, and the expression is not too
complicated, try to create a query with that expression... you could have a
table with several fields (A, B, C,...) and one formula field if you have
several expressions among the code that can be changed.

When you want to get a result, you fill the values on the several needed
fields of a specific record, then you get the formula from it's field and
then when you create the command, you use something like:

formula = "(A+B)^2"
command = "SELECT " & formula & " AS RESULT FROM mytable"

I hope it works...

Alberto Silva

Thanks, but I don't know what assurances I will have about the
expressions at this point. In any case, thanks for the responses
everybody.
 
R

Rob G

Have you tried using a DataColumn to do the evaluation for you. We use the
code something like the following in the full framework:

Private Function Evaluate(ByVal expr As String) As Object
If Len(expr) = 0 Then expr = "0"
Dim dt As New DataTable()
Try
dt.Columns.Add(New DataColumn("ExpressionResult",
Type.GetType("System.Decimal"), expr))
Catch err As Exception
Throw New ApplicationException(err.Message)
End Try
Dim dr As DataRow = dt.NewRow()
Try
dt.Rows.Add(dr)
Catch err As System.Data.EvaluateException
If err.Message.IndexOf("Infinity") > 0 Then
Throw New ApplicationException("Cannot divide by 0.")
Else
Throw New ApplicationException(err.Message)
End If
Catch err As Exception
Throw New ApplicationException(err.Message)
End Try
Return dt.Rows(0).Item(0)

End Function

Rob Giorgi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top