Board index » cppbuilder » TVariant and Variant

TVariant and Variant

When I create a method using DCOM that has Variants, they always get
converted to TVariants.  Many of the methods of Variant such as
GetElement() do not work on TVariants.  How can I get around this?
Example:
MyMethod(TVariant *vData)
{
    char cByte;

    cByte = vData.GetElement(0);  //Does not compile

Quote
}

Thanks,
Terry Neckar
 

Re:TVariant and Variant


Terry,

Get/PutElement are only method of the Variant [what we call the VCL variant]
class. You can now convert back and forth between the two variant types.
Actually, if the variant holds a SAFEARRAY, the data won't even be copied:
SAFEARRAY holds a pointer to the data, not the data itself. So, something
along following will do:

    Variant vclVar = *vData;
     cByte = vclVar.GetElement(i);

Note that you can also use a TSafeArray<> for a more natural array feel
[i.e. the subscript operator]. The beauty of TSafeArray<> really shows when
dealing with multiple dim. arrays. But it can also be used on single dim.
ones. For example,

     TSafeArrayUChar1 array(LPSAFEARRAY(*vData));
      for (int i=0; i<array.BoundsLength[0]; i++)
         ShowChar(array[i]);

If the SAFEARRAY holds a structure, it's then best to lock it and copy the
data. Greydanus posted a sweet template that encapsulates this recently. See
the thread "Structured data: SAFEARRAY, BSTR or what?". I would only suggest
that the template uses 'GetBaseVariant' before locking - just in case what
you really have is a variant that points to another variant that then really
contains the data.

I hope the above helps,

Bruneau.

Quote
Terry Neckar wrote in message <37614A87.E4F03...@microcoders.com>...
>When I create a method using DCOM that has Variants, they always get
>converted to TVariants.  Many of the methods of Variant such as
>GetElement() do not work on TVariants.  How can I get around this?
>Example:
>MyMethod(TVariant *vData)
>{
>    char cByte;

>    cByte = vData.GetElement(0);  //Does not compile
>}

>Thanks,
>Terry Neckar

Other Threads