Export (0) Print
Expand All

Variant Data Type

Microsoft Dynamics Nav 2009

The C/AL variant data type can contain any variants from OCX and Automation objects (VT_VARIANT). However, not all these values can be mapped to C/AL values. For more information, see Using COM Technologies in C/SIDE.

The variant data type can contain the following C/AL data types:

  • Record

  • File

  • Action

  • Codeunit

  • Automation

  • Boolean

  • Option

  • Integer

  • Decimal

  • Char

  • Text

  • Code

  • Date

  • Time

  • Binary

  • DateFormula

  • TransactionType

  • InStream

  • OutStream

You can use the variant data type to pass Automation variants from one external component (Automation or OCX) to another. This requires that the recipient component can accept the original variant. You can also assign a C/AL variable to a variant and pass it to an external component. When you pass C/AL variants ByRef to an external COM component, small conversion differences may occur. Therefore, we recommend that you pass C/AL variants ByVal if you do not need to assign a new value to the C/AL variant in your external component.

The C/AL variant is a complex data type. To return C/AL variants in function calls, you must pass them in a parameter ByVar (called ByRef in COM).

In the Classic client, if an Automation method returns a DateTime in a variant, then the time component is removed when the variant variable is converted to a typed variable. If an Automation method returns a value in a method argument, then the time component is not removed. For example, the following code calls an Automation method that returns a variant and returns a value in one of the arguments of the method. This example requires that you create the following variables.

Variable Data type

varAutomation

Automation

varVariant

Variant

returnVariant

Variant

outVariant

Variant

varDate

Date

varTime

Time

varDateReturn

Date

varDateOut

Date

// Create a variant that contains date and time.
varVariant := DATI2VARIANT(varDate, varTime);
// Call an automation method that takes two VT_DATE arguments:
// varVariant and outVariant. 
// The method returns a value in the outVariant argument.
returnVariant := varAutomation.MethodDateData(varVariant, outVariant);
varDateReturn := returnVariant;
varDateOut := VARIANT2DATE(outVariant);

In this example, you must convert the returnVariant and the outVariant variables to Date variables in different ways. You can assign returnVariant, which is the variant that the method returns, directly to a Date variable because the time component is removed automatically. You must call VARIANT2DATE on outVariant, which is the method argument, because the time component is not removed.

In the RoleTailored client, the time component is not removed from either a variant return value or a variant method argument when the variant variable is converted to a typed variable.

A C/AL variant contains two allocation areas: one for C/AL variables and one for Automation and OCX variants. This means that every time you assign a C/AL variable to a variant, the variant will contain the same data as the original variable. The conversion process does not change the data in any way. However, this is not the case with DATI2VARIANT because it is a VT_VARIANT. When you assign an external variable to a variant, the variant will also contain the same data as the original external variable.

In Microsoft Dynamics NAV 2009, if an automation method returns a byte string (bstr) in a variant, you cannot assign that variant directly to a field that is a Code data type. Instead, you must first assign the variant to a code variable, and then assign the code variable to the FieldRef value. For example, the following code assigns a variant from an automation method to a variable, and then assigns the variable to a field.

CodeVariable := AutomationMetodReturningBStrInVariant();
FieldRef.Value := CodeVariable;

In previous versions of C/SIDE, you could assign the variant directly to a Code FieldRef value.

Community Additions

ADD
Show:
© 2014 Microsoft