COMVariant.variant Method
Gets or sets the value of a COMVariant object of the VT_VARIANT (variant) data type.
Syntax
public COMVariant variant([COMVariant newValue])
Run On
Called
Parameters
- newValue
Type: COMVariant Class
The new value; optional.
Return Value
Type: COMVariant Class
The current value.
Remarks
The variant property is used to nest one COMVariant object in another COMVariant object. When using the parent object as the argument in a call to COMDispFunction.call or in a call to the COM class, the called method will automatically extract the data of the nested object. This nesting facility is useful when a method on a COM object can work with multiple data types. Only one level of variant nesting is allowed.
If you pass in a value that has a different data type than the object, the data type of the object will be changed to match the data type of the value.
A COMVariant object has a variant type if its data type is set to COMVariantType::VT_VARIANT.
Examples
The following example creates a COMVariant object of type VT_I4 (long), and a COMVariant object of type VT_VARIANT. The object of type VT_VARIANT is assigned the value of the object of type VT_I4.
Note
The code below contains a hypothetical COM object ("MyCOM.Object"), and will therefore not run in Microsoft Dynamics AX, unless such an object is created outside Microsoft Dynamics AX.
{
COM com;
COMDispFunction funcShow;
COMVariant var1 = new COMVariant(
COMVariantInOut::IN_OUT,
COMVariantType::VT_I4);
COMVariant var2 = new COMVariant(
COMVariantInOut::IN_OUT,
COMVariantType::VT_VARIANT);
InteropPermission perm1;
InteropPermission perm2;
// Set code access permission to help protect use of COM object
perm1 = new InteropPermission(InteropKind::ComInterop);
perm1.assert();
com = new COM("MyCOM.Object");
// Close code access permission for COM
CodeAccessPermission::revertAssert();
// Set value of 'var1'
var1.Long(123456);
// Set value of 'var2' to 'var1'
var2.Variant(var1);
// Set code access permission to protect use of
// COMDispFunction object
perm2 = new InteropPermission(InteropKind::ComInterop);
perm2.assert();
funcShow = new COMDispFunction(
com,
"Show",
COMDispContext::METHOD);
// Call funcShow with a long
funcShow.Call(var2);
// Change value of 'var1'
var1.BStr("Hello World");
// Call funcShow with a string
funcShow.Call(var2);
// Close code access permission for COMDispFunction
CodeAccessPermission::revertAssert();
}