Performing Calculations From a Text string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a string, "(5+3)/8" or "23>9"
Has .NET provides some sort of parser that can get me the mathematical answer?

Years ago, in VB6 I found an answer to this question using some sort of
web-based object. It's been so long, I forget even what it was.
 
I found my own answer if no one else has one. I use a Datatable to do the
computation for me:

Sub Main()
Dim B As Boolean = False
B = CBool(Compute("7=7", B.GetType))
Dim N As Decimal = 0
N = CDec(Compute("8+3.4", N.GetType))
Dim S As String = String.Empty
S = CStr(Compute("'Me ' + 'You'", S.GetType))
MsgBox(B.ToString + vbCrLf + N.ToString + vbCrLf + S)
End Sub

Private Function Compute(ByVal S As String, ByVal ResultType As Type) As
Object
Dim DT As New DataTable
DT.Columns.Add(New DataColumn("A", ResultType))
DT.Rows.Add(DT.NewRow)
DT.Columns("A").Expression = S
Return DT.Rows(0)("A")
End Function
 

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

Back
Top