NCalc expression evaluation error for custom function

2.1k Views Asked by At

I have an expression which has all calls to all custom functions.

IIF(FVAL(DES_CODE)=1039 OR FVAL(DES_CODE)=1040 OR FVAL(DES_CODE)=1034,0,TBLEVA(PTAX, FVAL(HOPO_GRS)))

When trying to evaluate this expression the NCalc engine is producing an error. I'm not able to figure out why. All function declared in the expression has been handled in the EvalFunction event of the expression. Then why is this error arising?

mismatched input 'OR' expecting ')' at line 1:24 missing EOF at 'OR' at line 1:47

CODE -

This is where I'm initializing the expression engine.

Dim exp As New Expression(vbCode, EvaluateOptions.IgnoreCase)
AddHandler exp.EvaluateParameter, AddressOf EvalParameters
AddHandler exp.EvaluateFunction, AddressOf EvalFunction
xReturn = Convert.ToDecimal(exp.Evaluate())

These are the events for the expression.

Public Sub EvalFunction(ByVal name As String, ByVal args As FunctionArgs)
    Select Case name.ToUpper.Trim
        Case "FVAL"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.Fval(xobj(0))
        Case "ORIGINALVAL"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.ORIGINALVAL(xobj(0))
        Case "IIF"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.IIF(xobj(0), xobj(1), xobj(2))
        Case "LOANDEDUCT"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.LOANDEDUCT(xobj(0))
        Case "TBLEVA"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.TBLEVA(xobj(0), xobj(1))
        Case "PERIODMONTH"
            args.Result = fc.PERIODMONTH()
        Case "PERIODSTART"
            args.Result = fc.PERIODSTART()
        Case "AGE"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.Age(xobj(0))
        Case "INT"
            Dim xobj = args.EvaluateParameters()
            args.Result = fc.Age(xobj(0))
    End Select
End Sub
Public Sub EvalParameters(ByVal name As String, ByVal args As ParameterArgs)
    args.Result = name
End Sub
1

There are 1 best solutions below

3
On BEST ANSWER

Just replace ORs with || or or(lowercase)

IIF(FVAL(DES_CODE)=1039 || FVAL(DES_CODE)=1040 || FVAL(DES_CODE)=1034,0,TBLEVA(PTAX, FVAL(HOPO_GRS)))