Name

VarArrayLock Function

Syntax

function VarArrayLock(var V: Variant): Pointer;

Description

VarArrayLock locks the Variant array’s dimensions so it cannot be resized, and returns a pointer to a simple array of the Variant array’s contents. The data array is organized such that the leftmost index varies first (row-major order). Note that ordinary Pascal arrays are stored in column-major order, so the rightmost index varies first.

VarArrayLock is a real function.

Tips and Tricks

  • If you need to perform an operation on all the elements of the Variant array, lock the array first to enhance performance.

  • The data array contains the actual values of each element of the Variant array. Verify that the array element type is what you expect, and cast the data pointer to the correct data type.

  • While an array is locked, you cannot change its size, but you can change its contents. Changes to the array elements are reflected in the raw data array that VarArrayLock returns.

  • You can lock an array more than once. To unlock the array, you must call VarArrayUnlock once for every time you call VarArrayLock.

Example

// Swap two doubles. procedure DSwap(var A, B: Double); var Tmp: Double; begin Tmp := A; A := B; B := Tmp; end; // Transpose a 2D matrix of varDouble values. For maximum performance, // lock the Variant array and access the raw data. procedure Transpose(var M: Variant); type TDoubleArray = array[0..MaxInt div SizeOf(Double)-1] of Double; PDoubleArray = ^TDoubleArray; var I, J: Integer; Data: PDoubleArray; ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.