Operators in Windows Forms Calculation Engine (Calculate)

13 Jul 20212 minutes to read

The calculation type of an equation will be specified by operators. These operators will be prioritized
in a default order for calculating the equation.

Arithmetic Operators

The mathematical operations such as addition, subtraction, multiplication etc., can be performed using the arithmetic operators.

Arithmetic operator denotation Example
+ (plus sign) Addition 1 + 2
- (minus sign - unary) Negation - 5
- (minus sign - binary) Subtraction 4 - 2
* (asterisk) Multiplication 2 * 5
/ (forward slash) Division 10 / 2
^ (caret) Exponentiation 5 ^ 2

Logical Operators

The logical or comparison operators will be used to compare two values of the formula. The return value will be either True or False.
If you use a well-formed logical expression in a larger calculation, True evaluates to numerical 1 and False evaluates to numerical 0 for use in the calculations.

Logical operator denotation Example
< (less than sign) Less than 5 < A2
> (greater than sign) Greater than A1 > A2
= (Equal sign) Equal to A1 = 4
<= (Less than or equal) Less than or equal A3 <= (A4 + 2)
>= (Greater than or equal) Greater than or equal B1 >= 30
<>(Not equal) Not equal C2 <>10

Text concatenation operator (Binary literal operator)

Two or more text strings can be concatenated into one text using this operator.

Logical operator denotation Example
& (ampersand) Concatenation “Hello” & “World”

Operator precedence

All operations are subjected to the following hierarchy of operations. The level 1 operations are done first, followed by level 2, and so on. Within the same level, the operations are performed from
left to right in the order where they are encountered during the parsing of the formula.

  1. (Unary Minus)

  2. /

    • -
  3. < >= <= >= <>

  4. & (Concatenation)

When the default operators’ precedence need to be changed, then the parentheses will be used to explicitly indicate the operation order.

Examples for Operator precedence

Formulas Computed Value
= 6 / 2 + 1 4
= 6 / (2 + 1) 2
= 2 + 4 / 2 4
= (2 + 4) / 2 3

Equal Sign, the Formula Character

To indicate that a particular string should be treated as a formula, user must start the string with a special character, FormulaCharacter(“=”). This property is static, so you can change the formula character within your code.
It’s default value is the equal sign, (=).

In general, in order for Essential Calculate to recognize a string as containing a formula; the string is required to start with the FormulaCharacter.
There is one exception though, if you explicitly call a CalcEngine Parse method like ParseFormula or ParseAndComputeFormula, including the formula character as the first character in the passed string, is optional.

Square Brackets, indexers in CalcQuickBase class

If you are using a CalcQuickBase object to add calculation support to your business object, then you must use strings as indexers on the CalcQuickBase instance to get and set values.
To register the strings in CalcQuickBase, it must be enclosed within square brackets “[ ]”. Eg. [A]. These registered variable names are indexer keys.

CalcQuickBase calcQuick = new CalcQuickBase();
calcQuick["A"] = "5";
calcQuick["B"] = "6";
calcQuick["C"] = "11";