Answered Evaluating the Expression in VB.net

  • Tuesday, June 26, 2012 5:27 AM
     
     

    Hi...

    I need to evaluate the expression which is given in TextBox. The expression can be any math function or simple arithmetic expression. I am sure that there will be some function to do this directly because in older development software it is available. 

    Please help..

    Thanks and Regards..


    Shrinidhi Acharya

All Replies

  • Wednesday, June 27, 2012 5:56 AM
    Moderator
     
     

    Hi shrinidhi,

    Thanks for you post.

    As for you issue, I’d like to do some clarify.

    >>because in older development software it is available.

    Would you like to share more information about this? Do some math methods are available in old versions and not available or caused some exception at now?

    It will be better if you can share the code about the expression.

    I look forward your reply.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us


  • Wednesday, June 27, 2012 6:26 AM
     
     Answered
  • Thursday, June 28, 2012 2:22 PM
     
     Answered Has Code

    Hi,

    For a simple mathematical expression try the .Compute()

    method for a DataTable.

    '

    Add one Button and one TextBox to your Form to try this code please.>>

    '

    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            TextBox1.Text = "2*3+8"
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim dt As New DataTable
            Dim result As Decimal
    
            Try
                result = Convert.ToDecimal(dt.Compute(TextBox1.Text, ""))
                MessageBox.Show(result.ToString, "Result = ")
            Catch ex As Exception
                MessageBox.Show(ex.ToString)
            End Try
    
        End Sub
    
    End Class

     



    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

  • Thursday, June 28, 2012 4:44 PM
    Moderator
     
     

    The datatable or script host solutions are probably the most efficient.

    A CodeDom solution might be more versitle but will incur more overhead.  A full parser/lexer that supports all possible expression syntax could be a lot of work, could be slow if it is not implemented properly, and would be overkill for simple expressions.

    If you expect all of the expressions to be simple, use the datatable as suggested by John Oliver.  For more complex expressions, the script host is probably the way to go.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

  • Thursday, June 28, 2012 4:45 PM
    Moderator
     
     
    dangit .paul._ I wondered why kept getting an error trying to propose this post!  You snuck in and beat me to it.  =)

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

  • Monday, July 02, 2012 5:15 AM
     
     

    Hi John Anthony Oliver,

    Your solution works superfine with arithmetic expression but if i need to insert mathematical expressions like sin(), cos() etc.. it does not work..

     


    Shrinidhi Acharya

  • Monday, July 02, 2012 6:25 AM
     
     

    Hi John Anthony Oliver,

    Your solution works superfine with arithmetic expression but if i need to insert mathematical expressions like sin(), cos() etc.. it does not work.

    If you want to use those types of functions you will need to look at some of the other options that have been suggested.   Each evaluator will support a different set of functionality.