Doing math in vb.net like Eval in javascript
Asked Answered
C

6

5

Is there any way to parse a string in vb.net (like, built in methods), that can do math like Eval can? For example, 3+(7/3.5) as a string would return 2.

I am not asking for you to code this for me, I just want to know if there is a built in way to do this, if there is not I will code it myself.

I can wager that it would not be able to parse stuff like Sin(90) on its own, and I understand that would need to be replaced by Math.Sin(90).

If there is a built in method, how do you use it?

Crossroad answered 20/9, 2009 at 23:10 Comment(3)
I don't have an answer. I don't believe there is any kind of parser like this in the .NET language (unless you do what Preet suggested). If you end up creating a parser check out ANTLR.Sustain
Probably gonna hand-code myself a parser at this point, its not that hard for basic math. The parenthesis thing may get a bit tricky tho....Crossroad
For parentheses, you can use recursion.Crabbe
D
16

There's a shortcut for limited (ie. simple) math expressions by using the DataTable.Compute method. Obviously, this isn't robust (limited functionality) and feels hackish to misuse the DataTable for this purpose, but I figured I would add to the current answers.

Example:

var result = new DataTable().Compute("3+(7/3.5)", null); // 5

"Sin(90)" wouldn't work with this approach. Refer to the DataColumn.Expression Property page for a list of supported functions, specifically under the "Aggregates" section.

Using the System.CodeDom namespace is an option.

Some helpful links:


EDIT: to address your comment, here is an approach to demonstrate replacing trigonometric functions with their equivalent Math class methods.

C#

string expression = "(Sin(0) + Cos(0)+Tan(0)) * 10";
string updatedExpression = Regex.Replace(expression, @"(?<func>Sin|Cos|Tan)\((?<arg>.*?)\)", match =>
            match.Groups["func"].Value == "Sin" ? Math.Sin(Int32.Parse(match.Groups["arg"].Value)).ToString() :
            match.Groups["func"].Value == "Cos" ? Math.Cos(Int32.Parse(match.Groups["arg"].Value)).ToString() :
            Math.Tan(Int32.Parse(match.Groups["arg"].Value)).ToString()
        );
var result = new DataTable().Compute(updatedExpression, null); // 10

VB.NET

Dim expression As String = "(Sin(0) + Cos(0)+Tan(0)) * 10"
Dim updatedExpression As String = Regex.Replace(expression, "(?<func>Sin|Cos|Tan)\((?<arg>.*?)\)", Function(match As Match) _
        If(match.Groups("func").Value = "Sin", Math.Sin(Int32.Parse(match.Groups("arg").Value)).ToString(), _
        If(match.Groups("func").Value = "Cos", Math.Cos(Int32.Parse(match.Groups("arg").Value)).ToString(), _
        Math.Tan(Int32.Parse(match.Groups("arg").Value)).ToString())) _
        )
Dim result = New DataTable().Compute(updatedExpression, Nothing)

Note, however, that you need to know the contents of the "arg" group. I know they are ints, so I used Int32.Parse on them. If they are a combination of items then this simple approach won't work. I suspect you will constantly need to band-aid the solution if it gets too complicated with more unsupported function calls, in which case the CodeDom approach or others may be more suitable.

Deroo answered 21/9, 2009 at 0:1 Comment(1)
It works, and you just saved me a TON of time coding. Ill just do what I was gonna do before with the Sin and that style of function. This is much, much, much more efficient than my class I was making lol.Crossroad
A
3

Here is a way to evaluate an expression that I haven't seen mentioned anywhere else: use a WebBrowser control and JavaScript's eval():

Option Strict On

Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)>
Public Class Form1
    Dim browser As New WebBrowser

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        browser.ObjectForScripting = Me
        'browser.ScriptErrorsSuppressed = True
        browser.DocumentText = "<script>function evalIt(x) { return eval(x); }</script>"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim result = browser.Document.InvokeScript("evalIt", New String() {"3+4*5"})
        If result IsNot Nothing Then
            MessageBox.Show(result.ToString())     '23
        End If
    End Sub
End Class
Anaphase answered 14/9, 2014 at 10:11 Comment(0)
B
0

This CodeProject article might do the trick:

An expression evaluator written in VB.NET
http://www.codeproject.com/KB/vb/expression_evaluator.aspx

There is also this:
http://www.dotnetspider.com/resources/2518-mathematical-expression-expression-evaluate-VB.aspx

Bagdad answered 20/9, 2009 at 23:18 Comment(5)
Its in vb.net 2003, and the conversion wizard broke it (see #1442525), so unless you could compile it into a .dll for me then I cannot use it. Thanks for trying tho!Crossroad
What about the second link? It actually uses Javascript to do the work for you.Bagdad
Just saw that link there, ill use that if my custom parser can't handle the job. I think I am gonna try that first at this point, it would be a good learning experience for me as I am still new to vb.Crossroad
Thanks! When I fail, at least I know I will have the same end result.Crossroad
Oh, dear, Robert. Oh dear.Sustain
I
0

One way would be to use the CodeDom namespace to compile it and execute it using reflection. May not be that performant, I don't know.

Inspector answered 20/9, 2009 at 23:36 Comment(1)
Refer to CodeDom Calculator in Ahmad's answer.Inspector
B
0

It's a bit late, but here is exactly what you wanted (make sure you parse the input to make sure there is no statement like 'something + vbcrlf + exec "format c:"' in it):

Usage:

MessageBox.Show(COR.Tools.EvalProvider.Eval("return 8-3*2").ToString())

  Class:

'Imports Microsoft.VisualBasic
Imports System


Namespace COR.Tools


Public Class EvalProvider


    Public Shared Function Eval(ByVal vbCode As String) As Object
        Dim c As VBCodeProvider = New VBCodeProvider
        Dim icc As System.CodeDom.Compiler.ICodeCompiler = c.CreateCompiler()
        Dim cp As System.CodeDom.Compiler.CompilerParameters = New System.CodeDom.Compiler.CompilerParameters

        cp.ReferencedAssemblies.Add("System.dll")
        cp.ReferencedAssemblies.Add("System.xml.dll")
        cp.ReferencedAssemblies.Add("System.data.dll")
        ' Sample code for adding your own referenced assemblies
        'cp.ReferencedAssemblies.Add("c:\yourProjectDir\bin\YourBaseClass.dll")
        'cp.ReferencedAssemblies.Add("YourBaseclass.dll")
        cp.CompilerOptions = "/t:library"
        cp.GenerateInMemory = True
        Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder("")
        sb.Append("Imports System" & vbCrLf)
        sb.Append("Imports System.Xml" & vbCrLf)
        sb.Append("Imports System.Data" & vbCrLf)
        sb.Append("Imports System.Data.SqlClient" & vbCrLf)
        sb.Append("Imports Microsoft.VisualBasic" & vbCrLf)

        sb.Append("Namespace MyEvalNamespace  " & vbCrLf)
        sb.Append("Class MyEvalClass " & vbCrLf)

        sb.Append("public function  EvalCode() as Object " & vbCrLf)
        'sb.Append("YourNamespace.YourBaseClass thisObject = New YourNamespace.YourBaseClass()")
        sb.Append(vbCode & vbCrLf)
        sb.Append("End Function " & vbCrLf)
        sb.Append("End Class " & vbCrLf)
        sb.Append("End Namespace" & vbCrLf)
        Debug.WriteLine(sb.ToString()) ' look at this to debug your eval string
        Dim cr As System.CodeDom.Compiler.CompilerResults = icc.CompileAssemblyFromSource(cp, sb.ToString())
        Dim a As System.Reflection.Assembly = cr.CompiledAssembly
        Dim o As Object
        Dim mi As System.Reflection.MethodInfo
        o = a.CreateInstance("MyEvalNamespace.MyEvalClass")
        Dim t As Type = o.GetType()
        mi = t.GetMethod("EvalCode")
        Dim s As Object
        s = mi.Invoke(o, Nothing)
        Return s
    End Function


End Class ' EvalProvider


End Namespace
Benzine answered 7/8, 2010 at 9:16 Comment(0)
E
-1

I don't know about VB.net built in, but we do stuff like this by linking in the IronPython runtime and passing it the expressions. This is way way faster that using reflection.

Emanuel answered 20/9, 2009 at 23:12 Comment(2)
My apologies. I interpreted "Eh?" as "What's IronPython?" when clearly you intended those two letters and a question mark to stand in for a more specific question. Since you already know what IronPython is, you also know that it's a dynamic language (like JavaScript) that can evaluate expressions (like JavaScript). You also must know that it runs on the .NET framework. What Preet Sangha was suggesting is that you could make a simple IronPython library to evaluate math expressions, and call it from your VB.NET code.Binnings
"Eh?" at the time of questioning should have been interpreted as "What's IronPython?", as you correctly assumed, but by the time you posted the link which explains how to google it, I had already done so yet had neglected to remark upon this fact. Please pardon my negligence in doing so, as I am sick right now and forgot to check the question right after I had finished said googling.Crossroad

© 2022 - 2024 — McMap. All rights reserved.