Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

Dimension, Declare

These two identical commands create new arrays or change the size of existing ones.

Usage

DIMENSION | DECLARE ArrayName( nRows [, nCols ] )
                    [, aArray2( nRows2 [, nCols2 ] )
                     ...]

FoxPro supports both one- and two-dimensional arrays. If only nRows is specified, a one-dimensional array is created. If both nRows and nCols are specified, you get a two-dimensional array. However, any two-dimensional array in FoxPro can be treated like a one-dimensional array at any time. See “DBF, FPT, CDX, DBC—Hike!” for more information on FoxPro’s weird (but convenient) array handling.

DIMENSION scopes arrays as private. The commands PUBLIC [ARRAY] and LOCAL [ARRAY] use the same arguments to create arrays of global and local scope, respectively. A classic example of dBloat. Either a new command PRIVATE [ARRAY], dropping the DIMENSION/DECLARE syntax, or preferably DIMENSION [ PUBLIC | PRIVATE | LOCAL ] would make more sense to us.

Arrays in FoxPro can use either parentheses (as shown above) or square brackets to enclose their row and column information. We recommend square brackets to avoid confusion with functions. (We only used parentheses in the syntax above because we’ve been using square brackets to indicate optional clauses.)

If ArrayName exists when you issue DIMENSION or DECLARE, it’s reshaped to match the new dimensions specified. Data is moved to the cell with the same element number as its original location (see AELEMENT() for an explanation of element numbers). Any new elements get a value of .F. Take a look at the aColCopy function under ACOPY() to see how to put the data back where it belongs when you add columns.

Example

DIMENSION aMyArray[3], aMyOtherArray[10,17]

* Add to an existing array
SELECT category FROM masterlist INTO ARRAY aCategory
DIMENSION aCategory[ ALEN(aCategory, 1) + 1 ]
aCategory[ ALEN(aCategory, 1) ] = "Other"

See Also

ACopy(), AElement(), ALen(), Array Manipulation, Local, Public