variant

You can use the variant type to store data types which are not known in advance.

For example, the AsyncGetProcessResult function retrieves the result of any running processes. The result type (for example, int, string, or double) is unknown and the data type of the return value is variant.

These BI# types can be stored in the variant type:

  • int
  • double
  • string
  • bool
  • DateTime
  • StringList
  • StringListList
  • VariantList

To use a value that does not support a specific variant type, you must convert the variant value to the concrete type. The conversion is done using the as operator:

string s = x as string;

To see the data type encapsulated in a variant, you must use the is operator. This enables you to cast the variant to a specific type:

variant v = 10;

if (v is string)  // returns false
{
    string sValue = v as string;
}

if (v is int)     // returns true
{
    int iValue = v as int;
}

If you try to convert variant to a particular type, and the type of encapsulated data is different, an exception is thrown:

variant v = "Some text....";

double dValue = v as double;     // this produces an exception

This example shows how to use variant when retrieving a result of asynchronous process:

variant vresult = AsyncGetProcessResult("afe123ophf");
if (vresult is string)
{
    string s = vresult as string;
}
else if (vresult is double)
{
    double d = vresult as double;
}