Framework / System Layer / Formula Engine / Variables

In This Topic
Variables
In This Topic

Variables are user defined identifiers to which you can assign a variant value. The variables which the NFormulaEngine recognizes are stored in an instance of the NVariableCollection class a reference to which can be obtained from the Variables property.

C#
Copy Code
NFormulaEngine engine = new NFormulaEngine();
NVariableCollection variables = engine.Variables;
Visual Basic
Copy Code
Dim engine As New NFormulaEngine
Dim variables As NVariableCollection = engine.Variables

In essence the variables collection is a hashtable, which assigns a name to a user provided variant value. Then the variant value can be used in the formula expression with its name. For example:

C#
Copy Code
// create an expression
string expression = "a + b";
// define variables
variables.Add("a", new NVariant(10));
variables.Add("b", new NVariant(20));
// evaluate expression - result is 30
NVariant res = engine.ParseAndEvaluate(expression);
// change a variable value
variables["a"] = new NVariant(20);
// evaluate expression - result is 40
res = engine.ParseAndEvaluate(expression);
Visual Basic
Copy Code
' create an expression
Dim expression As String = "a + b"
' define variables
variables.Add("a", New NVariant(10))
variables.Add("b", New NVariant(20))
' evaluate expression - result is 30
Dim res As NVariant = engine.ParseAndEvaluate(expression)
' change a variable value
variables.Item("a") = New NVariant(20)
' evaluate expression - result is 40
res = engine.ParseAndEvaluate(expression)
See Also