This formula evaluator can use different data types like
bool to calculate the value of a mathematical expression. You can use any of the operators, +, -, *, /, >, <, == etc., and specify parameters for them.
.NET doesn't support types like
Variant as in VB6 or Delphi. To avoid validating data type before doing any operation, I could create
Variant type that can be used to save data of different types and overload some operators that are frequently used, like bitwise +, unary + -, *,/.
This code works in 2 steps: First, parse the expression and transform it to Backward Poland Notation. Now every order item can be either an operator or an operand. Second, calculate value of order using stack. Variables are saved in a
static field, that has the
Hashtable type shown in the "string/value" view.
Using the code
To use this code, you must add a reference to the Evaluator.dll library.
Hashtable ht = new Hashtable();
Za.Evaluator.Evaluator.Variables = ht;
textBox1.Text = Za.Evaluator.Evaluator.Evaluate("[A] + [B]/1.2").ToString();
To realize addition functions, you need to edit
EmbeddedFunction in Calculator.cs file.