Statement PRIVATE Foundation

PRIVATE-Creates and initializes memory variables of the storage class PRIVATE.

Syntax
PRIVATE <VarName> [[:= <Expression>], ... ]
Parameters
<VarName>
<VarName> designates the name of a PRIVATE 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 sized by specifying a number within the element operator brackets, as in:
PRIVATE <aArray>[<nDimension1>, <nDimension2>,...] // or 
PRIVATE <aArray>[<nDimension1>][<nDimension2>]... 
If an array is declared in this form, all array elements are assigned the value NIL.
<Expression>
<Expression> is any legal expression whose value can optionally be assigned to the variable using the inline assignment operator (:=). If no assignment is made, the variable is initialized with the value NIL. If the variable was initialized as an array in the above described form, no assignment is can be made.
Several variables may be declared, as long as the variables, including assignment, are separated with commas.
Description

The PRIVATE statement creates memory variables of the storage class PRIVATE. Storage classes define the lifetime and the visibility of variables. With the PRIVATE statement, so-called "dynamic" memory variables are created at runtime and whose references the compiler does not resolve. The PRIVATE statement belongs to the executable statements and must always follow variable declarations such as MEMVAR, FIELD, LOCAL or STATIC.

The declaration of a PRIVATE variable takes place with the declaration MEMVAR. The generation of such a variable explicitly takes place with the PRIVATE statement or implicitly with assignment of a value to an undeclared variable. PRIVATE variables can also be created from character string expressions using the macro operator (&) at runtime. Since the symbolic variable name of PRIVATE variables exists at runtime, they can be saved in files and restored at runtime. This is not possible with LOCAL or STATIC variables.

<VarName> may occur only once within a function. This includes variables which are declared as STATIC or LOCAL. If a PRIVATE or PUBLIC variable of the name <VarName> already exists in a calling function, the existing variable is hidden and is no longer visible within the function or procedure in which the new PRIVATE variable of the same name was declared.

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

The lifetime of a PRIVATE variable extends implicitly to the lifetime of the function or procedure in which the variable was created. It is released after the function terminates, but can also be deleted with one of the commands CLEAR ALL, CLEAR MEMORY or RELEASE. A private variable remains visible in called functions and procedures until, if necessary, a new variable is created with the same name as the variable in the calling function or procedure.

A PRIVATE variable can be initialized when it is declared, meaning the value of any expression can be assigned to it with help of the inline assignment operator (:=), or it can be initialized by use of the array element operator [] with any dimension and size. If no assignment is done, PRIVATE variables have the value NIL after generation. When they are initialized as an array, no further assignment is possible and all the array elements have the value NIL.

Examples
PRIVATE variable declaration and usage

// In the example, PRIVATE variables are created at the beginning 
// explicitly with the PRIVATE statement. In the FOR...NEXT loop, 
// implicit PRIVATE variables are created by assignment, and the 
// variable name is established using the macro operator (10 PRIVATE 
// variables with the name xValue1 to xValue10). 

PROCEDURE Main 
   PRIVATE cVar, nNumber                // Variables with value NIL 
   PRIVATE aArray[10]                   // Array with NIL elements 
   PRIVATE 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") 

       &cVar:= nNumber                  // Generates variable 
   NEXT                                 // by assignment 

   SAVE TO Test ALL LIKE xValue*        // Stores dynamically 
                                        // generated variables 
   RELEASE ALL                          // Deletes all PRIVATEs 

   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.