Variant

From DataFlex Wiki
Jump to navigationJump to search

DataFlex supports the Variant Data Type.

This makes it convenient to work with external controls, such as ActiveX controls, where this data type is commonly used.

Data Access their official documentation for DAWs is here: Variant Language Guide and Variant Data Type

There's a number of interesting things you might bump into.

Functions that return a variant array

For example, having a control that returns a variant array.

I was working on some RSA logic and the ActiveX component I'm using does return the data via a variant array. The function looks like this:

Function ComDecryptBytesENC String llstr Returns Variant

After calling it you can see the result in the array by the debugger. It mentions that it is an array (hurray) and you got data back (w00t), but as DataFlex does not have a array count on it... it cannot deal with this. (D'oh) If you try to use (SizeOfArray(vVar)) on it then it barfs.

Yes I can get it returned in a string as well, but it's binary data so... a string isn't ideal, especially not with a character encoding messing up some of that when communicating with ActiveX controls, so the variant array was certainly promising. It even returned the correct number of bytes in the goody bag. Now if only I can open that goody bag.. that would be nice.

In code:

        Variant vMacKey
        Get ComDecryptBytesENC of hoRSA strEncryptedMacKey True to vMacKey

You can see in the debugger that you have a variant array of 16 elements, but there's no way to enumerate that.

Obviously the first thing I tried was:

        Variant[] vMacKey
        Get ComDecryptBytesENC of hoRSA strEncryptedMacKey True to vMacKey

Which compiled, but threw up the following error:

 Illegal Datatype Conversion. Incompatible data type - Cannot convert array of 'UCHAR[]' to array of 'VARIANT[]'
 
 Error: 4381

Hmm.. well DataFlex does all that autocasting stuff from one data type to another not? SO.. how about this then?

        UChar[] ucMacKey
        Get ComDecryptBytesENC of hoRSA strEncryptedMacKey True to ucMacKey

and low and behold that works!

On DataFlex 18.2 the debugger even understands it.

On DataFlex 18.0 it works, but you'll have to iterate through the data to see the binary values.

Determine Variant Type

DataFlex does not have a function for determining the native type.

Sometimes however that would be mighty nice to know as you can then adjust your code depending on the actual data in your variant variable.

Luckily Allan Greis Eriksen managed to unlock that functionality.

See: Getting the native type of a variant variable

ValueTree

Variants are also used in an advanced technique where you use the ValueTreeSerializeParameter and ValueTreeDeserializeParameter to move data from a struct to a ValueTree for general processing.

The ValueTree composite data type looks like this

Struct tValueTreeEx 
  Variant sValue 
  tValueTreeEx[] children 
End_Struct

(Beware that sValue here is of type Variant NOT string!)

The official documentation is here: see ValueTree Advanced usage

Sonny also briefly mentioned them here: Arrays & Structs in depth