Framework / System Layer / Formula Engine / Variants

Variants

Variants are value-type pairs, which facilitate the type conversion of values, as well as mathematical and logical operations with values of different type. Variants are represented by the NVariant class.

Each variant has a value, which can be obtained from its Value property. The variant value is an object from the type, which can be obtained from the Type property. The currently supported types of variant values are enumerated the VariantType enumeration.

 Variant Operators

The power of variants is their ability to provide transparent handling of type conversion so that you can perform common mathematical and logical operations, without caring for the actual type of the involved values. The NVariant class defines the following set of operators, which you can use:

  • Type Cast Operators - you can cast a variant to a value of specific type. For example:
    C#
    Copy Code
    // create a float variant
    NVariant v1 = new NVariant(1.5f);
    // cast to double value
    double d = (double)v1;
    
    Visual Basic
    Copy Code
    ' create a float variant
    Dim v1 As New NVariant(1.5F)
    ' convert to double value
    Dim d As Double = Convert.ToDouble(v1)
    
  • Unary Arithmetic Operators - you can use unary + and - operators. For example:
    C#
    Copy Code
    // create a float variant
    NVariant v1 = new NVariant(1.5f);
    // use unary minus - res is a float variant with value -1.5f
    NVariant res = -v1;
    
    Visual Basic
    Copy Code
    ' create a float variant
    Dim v1 As New NVariant(1.5F)
    ' use unary minus - res is a float variant with value -1.5f
    Dim res As NVariant
    res = NVariant.op_UnaryNegation(v1)
    
  • Binary Arithmetic Operators - you can use +, -, *, / and ^ operators. For example:
    C#
    Copy Code
    // create a float variant
    NVariant v1 = new NVariant(1.5f);
    // create an int variant
    NVariant v2 = new NVariant(3);
    // use binary plus - res is a float variant with value 4.5f
    NVariant res = v1 + v2;
    
    Visual Basic
    Copy Code
    ' create a float variant
    Dim v1 As New NVariant(1.5F)
    ' create an int variant
    Dim v2 As New NVariant(3)
    ' use binary plus - res is a float variant with value 4.5f
    Dim res As NVariant = NVariant.op_Addition(v1, v2)
    
  • Binary Comparison Operators - you can use >, <, >=, <=, == and != operators. For example:
    C#
    Copy Code
    // create a float variant
    NVariant v1 = new NVariant(1.5f);
    // create an int variant
    NVariant v2 = new NVariant(3);
    // use less operator - res is a boolean variant with value true
    NVariant res = (v1 < v2);
    
    Visual Basic
    Copy Code
    ' create a float variant
    Dim v1 As New NVariant(1.5F)
    ' create an int variant
    Dim v2 As New NVariant(3)
    ' use less operator - res is a boolean variant with value true
    Dim res As NVariant = NVariant.op_LessThan(v1, v2)
    
  • Binary String Operators - you can use & in your code, which uses variants. For example:
    C#
    Copy Code
    // create a float variant 
    NVariant v1 = new NVariant(1.5f);
    // create a string variant 
    NVariant v2 = new NVariant("Value is: ");
    // use & operator - res is a string variant with value "Value is: 1.5" 
    NVariant res = v2 & v1;
    
    Visual Basic
    Copy Code
    ' create a float variant 
    Dim v1 As New NVariant(1.5F)
    ' create a string variant 
    Dim v2 As New NVariant("Value is: ")
    ' use & operator - res is a string variant with value "Value is: 1.5" 
    Dim res As NVariant = NVariant.op_BitwiseAnd(v2, v1)
    
 Explicit Type Change
You can explicitly change the type of a variant with the help of the ChangeType method. This method will appropriately convert the contained value to the new type.
See Also