Statement PUBLIC Foundation

PUBLIC-Creates and initializes memory variables.

Syntax
PUBLIC <VarName> [[:= <Expression>], ... ]
Parameters
<VarName>
<VarName> designates the name of a PUBLIC variable which is to be created. If the array element operator immediately follows <VarName>, an array is assigned to the variable. The array can be dimensioned by specifying a number of elements, as in:
PUBLIC <aArray>[<nDimension1>, <nDimension2>,...] // or 
PUBLIC <aArray>[<nDimension1>][<nDimension2>]... 
If an array is declared in this form, all array elements have the value NIL.
<Expression>
<Expression> is any expression whose value can be assigned to the variable using the inline assignment operator (:=). If no assignment is made, the variable is initialized with the value .F. (false). If the variable was initialized as an array, no assignment can be made.
More than one variable may be declared as long as the variable names, including assignment, are separated by commas.
Description

The PUBLIC statement generates variables which belong to the storage class PUBLIC. Storage classes define the lifetime and visibility of variables. With the PUBLIC statement, so called "dynamic" variables are created at runtime. The PUBLIC statement belongs to the executable statements and must follow declarations of MEMVAR, FIELD, LOCAL or STATIC.

The declaration of a PUBLIC variable takes place with the declaration MEMVAR. The creation of such a variable takes place with the statement PUBLIC. Since the symbolic names of PUBLIC variables exist at runtime, they can be saved in files with the SAVE command and can be restored at runtime. PUBLIC variables can also be dynamically created from character string expressions using the macro operator (&) at runtime. This cannot be done with LOCAL or STATIC variables.

The variable name <VarName> may occur only once within a function. This includes variables which are declared as STATIC or as LOCAL. If a PRIVATE or a PUBLIC variable which has the variable name <VarName>already exists in a higher ordered function, the new PUBLIC variable is not created, but the PRIVATE or PUBLIC variable in the higher ordered function or procedure is referenced. That means that PUBLIC variables can be "hidden" by PRIVATE variables, although the reverse is not possible.

The length of a variable name is unlimited, but only the first 255 characters are significant. A variable name consists of alphanumeric characters, and the first character may not be numeric. The underscore (_) is permitted as a special character.

The lifetime of a PUBLIC variable extends implicitly to the entire lifetime of a program. It is released upon program termination, but can also explicitly be deleted with one of the commands CLEAR ALL, CLEAR MEMORY or RELEASE. A PUBLIC variable remains globally visible. It can, if necessary, be "hidden" by a new variable with the same variable name which belongs to the storage class PRIVATE, LOCAL or STATIC. As soon as the "new" variable is no longer visible or no longer exists, the PUBLIC variable becomes visible again.

A PUBLIC variable can be initialized when it is created, meaning the value of any expression may be assigned to it by using the inline assignment operator (:=), or it can be initialized by using the array element operator [] as an array with any dimension or size. If no assignment is done, PUBLIC variables have the value .F. (false) after generation. That is an exception to the rule of the default value being NIL for non-initialized variables. When a PUBLIC variable is initialized as an array, no further assignment is possible and all array elements have the value NIL.

Examples
PUBLIC usage
// In the example, PUBLIC variables are generated at the beginning 
// with the statement PUBLIC. In the FOR...NEXT loop, PUBLIC 
// variables are generated by using the macro operator 
// and are initialized with the inline assignment operator. 
// (10 PUBLIC variables with the name xValue1 to xValue10) 

PROCEDURE Main 
   PUBLIC cVar, nNumber                 // Variables with value .F. 
   PUBLIC aArray[10]                    // Array with NIL elements 
   PUBLIC dDate := Date(), cString := "xValue" 

   FOR nNumber := 1 TO 10 
       aArray[nNumber] := 10 * nNumber  // Initializes 
                                        // array elements 

       cVar := cString + ;              // Generates variable 
               LTrim( Str(nNumber) )    // name ("xValue1..10") 

       PUBLIC &cVar:= nNumber           // Generates variable 
   NEXT                                 // with macro operator 

   SAVE TO Test ALL LIKE xValue*        // Saves dynamically 
                                        // generated variables 
   CLEAR MEMORY                         // Deletes all PUBLICs 

   RESTORE FROM Test                    // Rereads dynamically 
                                        // generated variables 


   ? xValue1                            // Result: 1 
   ? xValue10                           // Result: 10 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.