|
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
|