Try Math ParserTable of ContentsVB.NET Example
Visual Basic Example

Using Math Parser COM Component in VB

To use bcParser COM Component in Visual Basic, you need to tell your Visual Basic that this component exists. So that during design time it the Visual Basic IDE can show you hints about the component using it’s intellisense technology. To enable this, you need to select References under the Project menu option.

In the References dialog you should check the “bcParser n.m Type Library”. If you cannot find that option, it means that your bcParser COM component is not registered in Windows System Registry. Then you need to make sure you install it properly.

If you do fine till this point, you are all set to go:

In the evaluation download, there is a simple, one form, Visual Basic 6 project that demonstrates using the component. You can easily try it out after going through above steps. You can find the example Visual Basic project in the installation directory of the component.

Dim WithEvents Parser As BCPARSERLib.Parser
Private Sub Command1_Click()

On Error GoTo HandleError
    'create our parser object:
    Set Parser = New BCPARSERLib.Parser
   
    'create a custom function that takes two parameters:
    Parser.CreateOneParamFunc "one"
    Parser.CreateTwoParamFunc "two"
    Parser.Create3ParamFunc "three"
    Parser.CreateNParamFunc "myavg"
   
    Parser.Expression = Text1.Text
   
    'MsgBox "Expression is set fine. Expression is: " + Parser.Expression, vbOKOnly
   
    Parser.X = 7 'we set X, Y variables' values this way.
    Parser.Variable("X") = 8 'we set any variable's value this way.
    Parser.Variable("myvar") = 10 'this will also create a variable if it does not exist.
   
    If Parser.IsVariable("x") Then 'variable case does not matter.
        MsgBox "Yes, X is a variable.", vbOKOnly
    End If
    If Parser.IsVariableUsed("X") Then 'we can detect if it is used in the current expression.
        MsgBox "Yes, X is used in the expression.", vbOKOnly
    End If
    'Parser.IsVariableUsed("Y") would return false because Y is not used for example.
   
   
    'display variable value
    LabelXVal.Caption = Parser.X
   
    'Don't Allow Division by Zero:
    Parser.StrictFloatingPoint = True
       
    'get the result:
    Label1.Caption = Parser.Value
   
    'For i = 0 To 100000000
    '  Parser.Evaluate
    'Next
   
    Exit Sub
 
HandleError:
    MsgBox Err.Description, vbOKOnly
    MsgBox "Forgot to declare variable? " & Parser.GetInvalidPortionOfExpression(), vbOKOnly

End Sub
Private Sub Form_Load()
    'Text1.Text = "IF(X, SIN(X+1), Y)"
    Text1.Text = "one(X+1) + two(50, 100) + three(100,200,300)"
End Sub

Private Function Parser_OnExecNParamFunc(ByVal FuncName As String, params() As Variant) As Double
    If FuncName = "MYAVG" Then 'compute avarage
        Sum = 0
        For i = LBound(params) To UBound(params)
            Sum = Sum + params(i)
        Next
        Parser_OnExecNParamFunc = Sum / (UBound(params) - LBound(params))
    Else
        Parser_OnExecNParamFunc = 0
    End If
End Function

Private Function Parser_OnExecOneParamFunc(ByVal FuncName As String, ByVal param1 As Double) As Double
    MsgBox "Calculating func value for " + FuncName, vbOKOnly
    If (FuncName = "ONE") Then
        MsgBox "Param1 is " + Str(param1), vbOKOnly
        RetVal = param1 + 1
        MsgBox "RetVal is " + Str(RetVal), vbOKOnly
    End If
    Parser_OnExecOneParamFunc = RetVal
End Function

Private Function Parser_OnExecTwoParamFunc(ByVal FuncName As String, ByVal param1 As Double, ByVal param2 As Double) As Double
    MsgBox "Calculating func value for " + FuncName, vbOKOnly
    If (FuncName = "TWO") Then 'note that function name is upper case "MYCUSTOMFUNC"
        MsgBox "Param1 is " + Str(param1), vbOKOnly
        MsgBox "Param2 is " + Str(param2), vbOKOnly
        RetVal = param1 + param2
        MsgBox "RetVal is " + Str(RetVal), vbOKOnly
    End If
    Parser_OnExecTwoParamFunc = RetVal
End Function

Private Function Parser_OnExec3ParamFunc(ByVal FuncName As String, ByVal param1 As Double, ByVal param2 As Double, ByVal param3 As Double) As Double
    MsgBox "Calculating func value for " + FuncName, vbOKOnly
    If (FuncName = "THREE") Then
        MsgBox "Param1 is " + Str(param1), vbOKOnly
        MsgBox "Param2 is " + Str(param2), vbOKOnly
        MsgBox "Param3 is " + Str(param3), vbOKOnly
        RetVal = param1 + param2 + param3
        MsgBox "RetVal is " + Str(RetVal), vbOKOnly
    End If
    Parser_OnExec3ParamFunc = RetVal
End Function

 

Here is an ASP Sample Screen Shot:

When using in ASP, make sure that your IIS user account has access permissions to the component. You should ideally install the COM Component in a directory where IIS can access it. Otherwise you will see access permission errors in your ASP page. The prog ID to use in Server.CreateObject method for the Component is BcParser.Parser.

Happy coding!

webmaster@bestcode.com