menu

WinForms

  • Code Examples
  • Upgrade Guide
  • User Guide
  • Demos
  • Support
  • Forums
  • Download
Class ExpressionFieldEvaluator - API Reference

    Show / Hide Table of Contents

    Class ExpressionFieldEvaluator

    Encapsulates the code required to parse and compute formulas. Hash table properties maintain a Formula Library of functions as well as a list of dependent cells.

    You can add and remove library functions.

    Inheritance
    System.Object
    ExpressionFieldEvaluator
    Implements
    IExpressionFieldEvaluator
    Inherited Members
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    Namespace: Syncfusion.Grouping
    Assembly: Syncfusion.Grouping.Base.dll
    Syntax
    public class ExpressionFieldEvaluator : IExpressionFieldEvaluator

    Constructors

    ExpressionFieldEvaluator(TableDescriptor)

    Initializes a new instance of the ExpressionFieldEvaluator class.

    Declaration
    public ExpressionFieldEvaluator(TableDescriptor tableDescriptor)
    Parameters
    Type Name Description
    TableDescriptor tableDescriptor

    The table descriptor.

    Fields

    FormulaErrorStrings

    String array that holds the strings used in error messages within the Formula Engine.

    Declaration
    public string[] FormulaErrorStrings
    Field Value
    Type Description
    System.String[]
    Remarks

    If you want to change the error messages displayed within the Formula Engine, you can set your new strings into the appropriate position in the FormulaErrorStrings array. Here is the code that shows the default settings. You should assign your new strings to the corresponding positions.

    Examples

    Here is the code that shows position of each string in FormulaErrorStrings.

           public string[] FormulaErrorStrings = new string[]
           {
               "binary operators cannot start an expression",    //0
               "cannot parse",                                    //1
               "bad library",                                    //2
               "invalid char in front of",                        //3
               "number contains 2 decimal points",                //4
               "expression cannot end with an operator",        //5
               "invalid characters following an operator",        //6
               "invalid character in number",                    //7
               "mismatched parentheses",                        //8
               "unknown formula name",                            //9
               "requires a single argument",                    //10
               "requires 3 arguments",                            //11
               "invalid Math argument",                        //12
               "requires 2 arguments",                            //13
               "bad index",                                    //14
               "too complex",                                    //15
               "circular reference: ",                            //16
               "missing formula",                                //17
               "improper formula",                                //18
               "invalid expression",                            //19
               "cell empty",                                    //20
               "empty expression",                                //21
            "mismatched string tics",                        //22
            "named functions not supported in expressions",    //23
            "not a formula",                                //24
            "missing operand"                                //25
           };

    Properties

    Culture

    Gets the culture information.

    Declaration
    public CultureInfo Culture { get; }
    Property Value
    Type Description
    System.Globalization.CultureInfo

    FunctionNames

    A hash table whose keys are function names and whose values are LibraryFunction delegates.

    Declaration
    public Hashtable FunctionNames { get; }
    Property Value
    Type Description
    System.Collections.Hashtable

    IsValid

    Gets the value that indicates whether the formula or expression is valid or not

    Declaration
    public bool IsValid { get; }
    Property Value
    Type Description
    System.Boolean

    MaximumRecursiveCalls

    Specifies the maximum number of recursive calls that can be used to compute a value.

    Declaration
    public int MaximumRecursiveCalls { get; set; }
    Property Value
    Type Description
    System.Int32
    Remarks

    This property comes into play when you have a calculated formula cell that depends on
    another calculated formula that depends on another calculated formula and so on. If the number of 'depends on another formula' exceeds MaximumRecursiveCalls, you will see a Too Complex message displayed in the cell. The default value is 20, but you can set it higher or lower depending upon your expected needs. The purpose of the limit is to avoid a circular reference locking up your application.

    Methods

    AddFunction(String, ExpressionFieldEvaluator.LibraryFunction)

    Adds a function to the Function Library.

    Declaration
    public bool AddFunction(string name, ExpressionFieldEvaluator.LibraryFunction func)
    Parameters
    Type Name Description
    System.String name

    The name of the function to be added.

    ExpressionFieldEvaluator.LibraryFunction func

    The function to be added.

    Returns
    Type Description
    System.Boolean

    True if successfully added, False otherwise.

    Remarks

    LibraryFunction is a delegate the defines the signature of functions that you can add to the Function Library. Adding a custom function requires two steps. The first is to register a name and LibraryFunction delegate with the ExpressionFielEvaluator object. The second step is to add a method to your code that implements the LibraryFunction delegate to perform the calculations you want done.

    There are restrictions on the use Functions within expressions. Functions can only be used stand-alone. They cannot be used as part of a more complex expression. So, "Func([Col1], 2*[Col2]+1)" is a valid use of a function named Func that accepts two arguments. But "2 * Func([Col1], 2*[Col2]+1) + 1" is not valid. If you need to use functions in algebraic expressions, then first add an Expression field that uses the function stand-alone. Then in your algebraic expression, you can refer to this Expression field. Argument used in library function calls, can be any algebraic combination of fields and constants, but they cannot contain function references. During calculations, the arguments are fully evaluated before being passed into the method you implement.

    Examples

    In the sample below, ComputeFunc is the name of the method we add to our code to compute the function value. Func is the string name that we use in an expression to reference the custom function as in "Func([Col1], [Col2])".

    // step 1 - register the function name and delegate
    ExpressionFieldEvaluator evaluator = this.groupingEngine.TableDescriptor.ExpressionFieldEvaluator;//.CreateExpressionFieldEvaluator(this.groupingEngine.TableDescriptor);
    evaluator.AddFunction("Func", new ExpressionFieldEvaluator.LibraryFunction(ComputeFunc));
    

    // step 1 - defining the method // Computes the absolute value of arg1-2*arg2 // parameter s- a list of 2 arguments // returns string holding computed value public string ComputeFunc(string s) { //get the list delimiter (for en-us, its is a comma) char comma = Convert.ToChar(this.gridGroupingControl1.Culture.TextInfo.ListSeparator); string[] ss = s.Split(comma); if(ss.GetLength(0) != 2) throw new ArgumentException("Requires 2 arguments."); double arg1, arg2; if(double.TryParse(ss[0], System.Globalization.NumberStyles.Any, null, out arg1) && double.TryParse(ss[1], System.Globalization.NumberStyles.Any, null, out arg2)) { return Math.Abs(arg1 - 2 * arg2).ToString(); } return ""; }

    ' step 1 - register the function name and delegate
    Dim evaluator As ExpressionFieldEvaluator = Me.groupingEngine.TableDescriptor.ExpressionFieldEvaluator
    evaluator.AddFunction("Func", New ExpressionFieldEvaluator.LibraryFunction(AddressOf ComputeFunc)) 
    

    ' step 1 - defining the method ' Computes the absolute value of arg1-2*arg2 ' parameter s- a list of 2 arguments ' returns string holding computed value Public Function ComputeFunc(s As String) As String 'get the list delimiter (for en-us, its is a comma) Dim comma As Char = Convert.ToChar(Me.gridGroupingControl1.Culture.TextInfo.ListSeparator) Dim ss As String() = s.Split(comma) If ss.GetLength(0) <> 2 Then Throw New ArgumentException("Requires 2 arguments.") End If Dim arg1, arg2 As Double If Double.TryParse(ss(0), System.Globalization.NumberStyles.Any, Nothing, arg1) _ AndAlso Double.TryParse(ss(1), System.Globalization.NumberStyles.Any, Nothing, arg2) Then Return Math.Abs((arg1 - 2 * arg2)).ToString() End If Return "" End Function 'ComputeFunc

    ComputeFormulaValueAt(String)

    Compute values for the record of inner formulas.

    Declaration
    public string ComputeFormulaValueAt(string formula)
    Parameters
    Type Name Description
    System.String formula

    The pre-compiled formula expression.

    Returns
    Type Description
    System.String

    The resulting value.

    ComputeFormulaValueAt(String, Record)

    Compute values for the record in the data source.

    Declaration
    public string ComputeFormulaValueAt(string formula, Record position)
    Parameters
    Type Name Description
    System.String formula

    The pre-compiled formula expression.

    Record position

    The record.

    Returns
    Type Description
    System.String

    The resulting value.

    ComputeFormulaValueAt(String, Record, String)

    Compute values for the record in the data source.

    Declaration
    public string ComputeFormulaValueAt(string formula, Record position, string expressionName)
    Parameters
    Type Name Description
    System.String formula

    The pre-compiled formula expression.

    Record position

    The record.

    System.String expressionName

    The name of the expression being computed.

    Returns
    Type Description
    System.String

    The resulting value.

    GetValueFromDataTable(String)

    Returns the value for the specified field / token from the record.

    Declaration
    public virtual string GetValueFromDataTable(string token)
    Parameters
    Type Name Description
    System.String token

    The column token.

    Returns
    Type Description
    System.String

    The value from the record.

    InitializeTableFormulaTokens()

    Loads item properties from the table descriptor and creates tokens that can be used in compiled expressions.

    Declaration
    public void InitializeTableFormulaTokens()

    IsExpressionValid(String, String, Record, out String)

    Checks if an expression formula is valid for a particular host expression based on the record.

    Declaration
    public bool IsExpressionValid(string expressionFieldName, string formula, Record record, out string errorString)
    Parameters
    Type Name Description
    System.String expressionFieldName

    The name of the host ExpressionFieldDescriptor.

    System.String formula

    The formula.

    Record record

    The record which has data to validate the expression.

    System.String errorString

    Returns the error string if expression is invalid.

    Returns
    Type Description
    System.Boolean

    True if formula is valid, false otherwise.

    IsExpressionValid(String, String, out String)

    Checks if an expression formula is valid for a particular host expression.

    Declaration
    public bool IsExpressionValid(string expressionFieldName, string formula, out string errorString)
    Parameters
    Type Name Description
    System.String expressionFieldName

    The name of the host ExpressionFieldDescriptor.

    System.String formula

    The formula.

    System.String errorString

    Returns the error string, if any.

    Returns
    Type Description
    System.Boolean

    True if formula is valid, false otherwise.

    PutTokensInFormula(String)

    Replaces column references with tokens.

    Declaration
    public string PutTokensInFormula(string formula)
    Parameters
    Type Name Description
    System.String formula

    The formula expression.

    Returns
    Type Description
    System.String

    A prepared expression string.

    RemoveFunction(String)

    Removes a function from the Function Library.

    Declaration
    public bool RemoveFunction(string name)
    Parameters
    Type Name Description
    System.String name

    The name of the function to be removed.

    Returns
    Type Description
    System.Boolean

    True if successfully removed, False otherwise.

    ToString()

    Displays information on the cell currently being calculated.

    Declaration
    public override string ToString()
    Returns
    Type Description
    System.String

    String with information on the cell currently being calculated.

    Overrides
    System.Object.ToString()

    Explicit Interface Implementations

    IExpressionFieldEvaluator.Parse(String, Boolean)

    Used to split the string

    Declaration
    string IExpressionFieldEvaluator.Parse(string text, bool isRecordFilter)
    Parameters
    Type Name Description
    System.String text

    Gets the string

    System.Boolean isRecordFilter

    Determine whether its record filter or not.

    Returns
    Type Description
    System.String

    Returns the parsed string.

    Implements

    IExpressionFieldEvaluator
    Back to top Generated by DocFX
    Copyright © 2001 - 2023 Syncfusion Inc. All Rights Reserved