PUBLIC Command

Defines global variables or arrays.

PUBLIC MemVarList

-Or-

PUBLIC [ARRAY] ArrayName1(nRows1 [, nColumns1])
   [, ArrayName2(nRows2 [, nColumns2])] ... 
   [AS type [OF ClassLib]]

Parameters

  • MemVarList
    Specifies one or more memory variables to be initialized and designated as global.
  • [ARRAY] ArrayName1 (nRows1 [, nColumns1])   [, ArrayName2 (nRows2 [, nColumns2])] ...
    Specifies one or more arrays to be initialized and designated as global. See DIMENSION for a description of each argument.
  • AS type
    Specifies the data type on which this variable or array is based.
  • OF ClassLib
    Specifies the class library containing the type description on which the type element of this variable or array is based.

Remarks

Multiple items within MemVarList are separated by commas. Global variables and arrays can be used and modified from any program you execute during the current Visual FoxPro session.

Variables and arrays created with PUBLIC are initialized false (.F.) except for the public variables FOX and FOXPRO, which are initialized true (.T.). The public variables FOX and FOXPRO can be used to conditionally execute code based on the product you are running.

Any Variable or array you create in the Command window is automatically public.

Any Variable or array you wish to declare as public must be declared public prior to assigning it a value.

Visual FoxPro generates a syntax error if, within a program, you assign a value to a variable or array and later declare it public with PUBLIC.

The strong typing required by the CodeSense parser in IntelliSense is available only when you create strongly typed object and variable references by using the optional AS clause.

Example

SET TALK OFF
PUBLIC val1,val2
val1 = 10
val2 = 15

DO down
? val1
? val2

RELEASE ALL     && Releases private variables only
DISPLAY MEMORY LIKE val?
RELEASE val1,val2  && Public variables must be released explicitly
DISPLAY MEMORY LIKE val?

PROCEDURE down
PRIVATE val1
val1 = 50
val2 = 100
? val1
? val2
RETURN

See Also

DIMENSION | FUNCTION | LOCAL | LPARAMETERS | PARAMETERS | PARAMETERS( ) | PRIVATE | PROCEDURE | RELEASE