VarPrint Function

Requires: Variants.lg32

Purpose

Returns the contents of a Variant and Variant Array

Syntax

output = VarPrint(value[, options])

options: Integer
output: String
variant: Variant

Description

VarPrint returns the contents of a Variant as a string: if the Variant has a string or numerical value, this is returned; if not, if it is an Object type and the object is named, this is returned; otherwise the variable type of the Variant is returned.

When dealing with a Variant Array, by default, VarPrint simply returns the variable type of the array; however, if the VARPRINT_ARRAYINFULL constant is passed in the options parameter, the function returns the contents of the array as a tree. Hence, the array created by the following declaration...

Dim a = Array(1, 2.2, Array("GFABASIC", 3))

...is represented in treeform as follows:

Variant() array
|- [0] 1 [Integer]
|- [1] 2.2 [Double]
|- [2] - Variant() array
| |- [0] GFABASIC [String]
| |- [1] 3 [Integer]

The first figure in the square brackets is the array element number, followed by either the element value then its variable type in square brackets or the variable type in normal brackets; if the array is of a specific variable type (i.e. not Variant), the description of the variable type in square brackets following the value is omitted. Embedded arrays are represented by a description of the array variable type and its contents are shown on a new 'branch'. From version 3 of the library onwards, where the variant type is an Object, the type name of the object is now displayed as a value, if possible.

To better aid debugging, the constant VARPRINT_DEBUG was added in version 3 which adds the address of the values next to the variable type. Version 3 of the library also added better error handling so, if the variable address is corrupt or protected, error text is now added to the output rather than an error being raised as previous versions.

One final option is available - VARPRINT_OUTPUT - which prints the output to the screen at the cursor point.

Example

$Library "Variants.lg32"

Dim obj As Variant : Ocx TextBox txt : Set obj = txt

Dim b = Array(1, 2, 3) As Byte : VarReDim b, "2 to 4"

Dim c = Array("Hello", "Goodbye") As String , d? = True

Dim a = Array(1, 2.2, d?, Array("Dog", "Cat", b, c, 144.56), obj)

Debug VarPrint(a) : Debug

Debug VarPrint(a, VARPRINT_ARRAYINFULL)

Debug VarPrint(a(3), VARPRINT_ARRAYINFULL)

Debug VarPrint(a(3)(2), VARPRINT_ARRAYINFULL)

Debug VarPrint(a(3)(3), VARPRINT_ARRAYINFULL)

Debug.Show

See Also

Variant Arrays, VarLoad, VarSave.

{Created by James Gaite; Last updated: 26/11/2023 by James Gaite}