|
Option Strict Off
Option Explicit On
'imports BCPARSERLib
Friend Class Form1
Inherits System.Windows.Forms.Form
Dim Parser As BCPARSERLib.Parser
Private Sub Command1_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command1.Click
On Error GoTo HandleError
'create our parser object:
Parser = New BCPARSERLib.Parser
'In VB.NET, we need to register event handler in a different way that .NET COM Interoperability requires:
AddHandler Parser.OnExecOneParamFunc, New BCPARSERLib._IParserEvents_OnExecOneParamFuncEventHandler(AddressOf Parser_OnExecOneParamFunc)
AddHandler Parser.OnExecTwoParamFunc, New BCPARSERLib._IParserEvents_OnExecTwoParamFuncEventHandler(AddressOf Parser_OnExecTwoParamFunc)
AddHandler Parser.OnExec3ParamFunc, New BCPARSERLib._IParserEvents_OnExec3ParamFuncEventHandler(AddressOf Parser_OnExec3ParamFunc)
AddHandler Parser.OnExecNParamFunc, New BCPARSERLib._IParserEvents_OnExecNParamFuncEventHandler(AddressOf Parser_OnExecNParamFunc)
'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.", MsgBoxStyle.OKOnly)
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.", MsgBoxStyle.OKOnly)
End If
'Parser.IsVariableUsed("Y") would return false because Y is not used for example.
'display variable value
LabelXVal.Text = CStr(Parser.X)
'Don't Allow Division by Zero:
Parser.StrictFloatingPoint = True
'get the result:
Label1.Text = CStr(Parser.Value)
'For i = 0 To 100000000
' Parser.Evaluate
'Next
Exit Sub
HandleError:
MsgBox(Err.Description, MsgBoxStyle.OKOnly)
MsgBox("Forgot to declare variable? " & Parser.GetInvalidPortionOfExpression(), MsgBoxStyle.OKOnly)
End Sub
Private Sub Form1_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.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, ByRef params As System.Array) As Double
Dim i As Integer
Dim Sum As Double
If FuncName = "MYAVG" Then 'compute avarage
Sum = 0
For i = LBound(params) To UBound(params)
Sum = Sum + params(i)
Next
Parser_OnExecNParamFunc = 1 '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
Dim RetVal As Double
MsgBox("Calculating func value for " & FuncName, MsgBoxStyle.OKOnly)
If (FuncName = "ONE") Then
MsgBox("Param1 is " & Str(param1), MsgBoxStyle.OKOnly)
RetVal = param1 + 1
MsgBox("RetVal is " & Str(RetVal), MsgBoxStyle.OkOnly)
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
Dim RetVal As Double
MsgBox("Calculating func value for " & FuncName, MsgBoxStyle.OKOnly)
If (FuncName = "TWO") Then 'note that function name is upper case "MYCUSTOMFUNC"
MsgBox("Param1 is " & Str(param1), MsgBoxStyle.OKOnly)
MsgBox("Param2 is " & Str(param2), MsgBoxStyle.OKOnly)
RetVal = param1 + param2
MsgBox("RetVal is " & Str(RetVal), MsgBoxStyle.OkOnly)
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
Dim RetVal As Double
MsgBox("Calculating func value for " & FuncName, MsgBoxStyle.OKOnly)
If (FuncName = "THREE") Then
MsgBox("Param1 is " & Str(param1), MsgBoxStyle.OKOnly)
MsgBox("Param2 is " & Str(param2), MsgBoxStyle.OKOnly)
MsgBox("Param3 is " & Str(param3), MsgBoxStyle.OKOnly)
RetVal = param1 + param2 + param3
MsgBox("RetVal is " & Str(RetVal), MsgBoxStyle.OkOnly)
End If
Parser_OnExec3ParamFunc = RetVal
End Function
End Class
|