A simple expression evaluator

While any Windows user could pop up the Calculator accessory to perform any type of math calculations, it would be great if you could offer him or her the capability to do simple math from within your application. This is a very simple expression evaluator function that does it:

Function EvalExpression(ByVal expression As String) As Double    Dim result As Double    Dim operand As Double    Dim opcode As String    Dim index As Integer    Dim lastIndex As Integer        ' the null character mark the end of the string    expression = expression & vbNullChar        For index = 1 To Len(expression) + 1        If InStr("+-*/" & vbNullChar, Mid$(expression, index, 1)) Then            If lastIndex = 0 Then                ' this is the first operand in the expression                result = Val(Left$(expression, index - 1))            Else                ' extract the new operand                operand = Val(Mid$(expression, lastIndex, index - lastIndex))                ' execute the pending operation                Select Case opcode                Case "+"                    result = result + operand                Case "-"                    result = result - operand                Case "*"                    result = result * operand                Case "/"                    result = result / operand                End Select            End If            opcode = Mid$(expression, index, 1)            lastIndex = index + 1        End If    Next    EvalExpression = LTrim$(result)    End Function

This evaluator is very simple, and has a number of limitations: it does not account for negative numbers nor for parenthesis and sub-expressions, and operands are evaluated from left to right without any priority rule (e.g. “10+2*3” returns 36, and not 16). Nevertheless it is very handy and lets you make you program friendlier to your customer by adding the capability to perform calculation from within a textbox control. This routine shows how to evaluate the expression in a textbox control when the user presses the F2 key:

Sub Text1_KeyDown (KeyCode As Integer, Shift As Integer)    If KeyCode = vbKeyF2 And Shift = 0 Then        Text1.Text = EvalExpression(Text1.Text)        Text1.SelStart = Len(Text1.Text)    End IfEnd Sub

If you want to evaluate more complex expressions you can use the MS Script Control library, which offers a simple way to do it. This control has an Eval method that evaluates any expression, with parenthesis, sub-expressions, math functions such as sqr, power, etc., and evaluates the operators in the right order. Add the “Microsoft Script Control” library from the References dialog window, and execute the following code to calculate the result of the given sample expression:Dim oScriptCtl As New ScriptControlDim sExpr As String

oScriptCtl.Language = "VBScript"sExpr = "sqr(25)+2^2+((3+7)/5+1)"MsgBox sExpr & " = " & oScriptCtl.Eval(sExpr)

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may