Variant Array and Arrays of Variant

Description

Although Array of Variants and Variant Array may sound like the same thing, they are two distinct things:

The first - Array of Variants - is a standard GFABASIC array dimensioned to hold Variant type variables and created thus...

Dim var(10) [As Variant]

...while the other is a Array of any or multiple variable types stored inside a Variant type variable (from now on referred to simply as Variant) created using the Array function like this...

Dim var = Array(1, 2, 3) [As Type]

This last type of array is also known as a SafeArray or a Dynamic Array.

The main differences between a GFA array of Variants and a Variant array are speed, flexibility, compatibility and usability within GFABASIC programs:

Using GFABASIC Arrays of Variants is similar to using any other type of GFABASIC array - all the commands and functions work as normal - with the difference being in the vagaries of the Variant Type itself.

However, Variant Arrays are different, with some different keywords, and need further explanation.

Variant Arrays

There are only three GFABASIC keywords which work with Variant Arrays: Array, LBound and UBound. Added to these in version 2.58 onwards are VarArraySet and VarReDim, as well as VarLoad and VarSave which are universal Variant keywords.

Creating Variant Arrays

variant = Array([parameter list]) [As Type]

Show


Re-sizing Variant Arrays

Requires: Variants.lg32

VarReDim variant, elements%
VarReDim variant, "LBound TO UBound"
elem% = VarSize(variant array)

Show


Reading and Setting Elements in Variant Arrays

Requires: Variants.lg32

VarArraySet variant, index1[, index2, ....], value

Show


Inserting and Deleting Elements in Variant Arrays

Requires: Variants.lg32 version 3

VarInsert variant, elemno%, value [, options%]
VarDelete variant, elemno% [, options%]

Show


Other useful keywords are:

See Also

Variant Type.

{Created by James Gaite; Last updated: 26/11/2023 by James Gaite; Other Contributors: Jean-Marie Melanson}