Framework / System Layer / Formula Engine / References

In This Topic
References
In This Topic

References are user defined identifiers to which you can assign a variant value, while the expression is being evaluated. References are similar to variables, but the difference is that variables must be defined before you parse the expression, while references are resolved dynamically - while the expression is parsed.

If the formula engine encounters a valid identifier, which is not a function or variable name it will call the DelegateIsReferenceName delegate to query whether the specified identifier is a valid reference name. For each reference name for which the DelegateIsReferenceName delegate returned true, the DelegateGetReferenceValue must provide a valid variant value.

The following code demonstrates how to use these two delegates:

C#
Copy Code
... 

NFormulaEngine engine = new NFormulaEngine();
// hook delegates
engine.DelegateIsReferenceName = new IsReferenceName(IsReferenceName); 
engine.DelegateGetReferenceValue = new GetReferenceValue(GetReferenceValue);
// evaluate an expression in which the a and b identifiers are references
// the result will be 30
NVariant res = engine.ParseAndEvaluate("a+b");

...

private bool IsReferenceName(string name)
{
    if (name == "a")
  return true;

    if (name == "b")
  return true;

    return false;
}

private NVariant GetReferenceValue(string name)
{
    if (name == "a")
  return new NVariant(10);

    if (name == "b")
  return new NVariant(20);

    throw new Exception("Must not be called for other names");
}
Visual Basic
Copy Code
... 

Dim engine As New NFormulaEngine
' hook delegates
engine.DelegateIsReferenceName = New IsReferenceName(AddressOf IsReferenceName)
engine.DelegateGetReferenceValue = New GetReferenceValue(AddressOf GetReferenceValue)
' evaluate an expression in which the a and b identifiers are references
' the result will be 30
Dim res As NVariant = engine.ParseAndEvaluate("a+b")

...

Private Function IsReferenceName(ByVal name As String) As Boolean
    If name = "a" Then
        Return True
    End If

    If name = "b" Then
        Return True
    End If

    Return False
End Function

Private Function GetReferenceValue(ByVal name As String) As NVariant
    If Name = "a" Then
        Return New NVariant(10)
    End If

    If Name = "b" Then
        Return New NVariant(20)
    End If

    Throw New Exception("Must not be called for other names")
End Function
See Also