Working With Variants and Variant Arrays

Return To Contents

One powerful built-in type of many development environments is the Variant type. Variants represent values whose type is not determined at compile time. Instead, the type of their value can change at runtime. Variants can mix with other variants and with integer, real, string, and boolean values in expressions and assignments; the compiler automatically performs type conversions.

However, variants are not always the same between different compilers. This section will cover how to setup Variant Arrays for use with our components that take Variant parameters, mostly for our Plotting Components.

Note: remember that arrays indices are generally zero-based for many compilers. This means that dimensions and elements begin at zero. Dimension 0 is the first dimension, dimension 1 is the second dimension, and so on. Element 0 is the first element, element 1 is the second element, element 2 is the third element, etc.

Even though some compilers allow you to specify lower bounds at values other than 0, we recommend that you always use 0 as the first element. Some methods can handle elements that start at non-zero values, but will generally have problems with dimensions that start at non-zero values.

Borland Delphi

To create a Variant Array of One Dimension with a Fixed Set of 10 Elements...

TempVariant : Variant;

//Create Array and specify variant type code

//to be a double type

TempVariant := VarArrayCreate([0, 9], varDouble);

To create a Variant Array of Two Dimensions with a Fixed Set of 10 Elements per dimension...

TempVariant : Variant;

//Create Array and specify variant type code

//to be a double type

TempVariant := VarArrayCreate([0, 1, 0, 9], varDouble);

iPlotX Channel AddXYArray example. We need to pass a 2-dimensional variant array with the first dimension containing the X-coordinate Values and the second dimension containing the Y-coordinate Values of each data point. Think of the first dimension as the first column of rows in a spread-sheet, and the second dimension as the second column of rows in a spread-sheet. Each row in the spread-sheet represents an XCoordinate-YCoordinate pair making up a single-data point.

Element Number
First Dimension
Second Dimension
Data Point
0
5
74.5
(5,74.5)

10
32.7
(10, 32.7)

15
63.7
(15, 63.7)
...
...
...
...
98
495
22.2
(495, 22.2)
99
500
51.4
(500, 51.4)

procedure TForm1.Button1Click(Sender: TObject);

TempVariant : Variant;

x : Integer;

begin

TempVariant := VarArrayCreate([0, 1, 0, 99], varDouble);

//Loop 100 times

for x := 0 To 99 do

begin

//Populate 1st dimension of array with

//x loop value

TempVariant[0, x] := x;

//Populate 2nd dimenstion of array with

//random y-coordinate data

TempVariant[1, x] := Random(100);

end;

iPlot1.Channel[0].AddXYArray(TempVariant);

end;

Borland C++ Builder

To create a Variant Array of One Dimension with a Fixed Set of 10 Elements...

Variant TempVariant;

int bounds[2] = {0, 9};

//Create Array and specify variant type code

//to be a double type

TempVariant = VarArrayCreate(bounds, 1, varDouble);

To create a Variant Array of Two Dimensions with a Fixed Set of 10 Elements per dimension...

Variant TempVariant;

int bounds[4] = {0, 1, 0, 9};

//Create Array and specify variant type code

//to be a double type

TempVariant = VarArrayCreate(bounds, 3, varDouble);

iPlotX Channel AddXYArray example. We need to pass a 2-dimensional variant array with the first dimension containing the X-coordinate Values and the second dimension containing the Y-coordinate Values of each data point. Think of the first dimension as the first column of rows in a spread-sheet, and the second dimension as the second column of rows in a spread-sheet. Each row in the spread-sheet represents an XCoordinate-YCoordinate pair making up a single-data point.

Element Number
First Dimension
Second Dimension
Data Point
0
5
74.5
(5,74.5)

10
32.7
(10, 32.7)

15
63.7
(15, 63.7)
...
...
...
...
98
495
22.2
(495, 22.2)
99
500
51.4
(500, 51.4)

{

Variant TempVariant;

int bounds[4] = {0, 1, 0, 99};

TempVariant = VarArrayCreate(bounds, 3, varDouble);

//Loop 100 times

for (int i=0; i<100; i++)

{

//Populate 1st dimension of array with

//x loop value

TempVariant.PutElement(i, 0, i);

//Populate 2nd dimenstion of array with

//random y-coordinate data

TempVariant.PutElement(random(100), 1, i);

};

iPlot1->Channel[0]->AddXYArray(TempVariant);

}

Contents | Index | Previous | Next