Note | This command is not available in the Core and Vulcan dialects |
Purpose
Creates variables and arrays visible within current and invoked routines.
PRIVATE <idVar> [:= <uValue>] | <ArraySpec> [, ...]
PRIVATE <idVar> [:= <uValue>] [AS <Type> [OF <ClassLibrary>] ] // FoxPro dialect
<idVar> | A valid identifier name for the private variable to create. |
<uValue> | The initial value to assign to the variable. If not specified, the variable is initialized to NIL. |
<ArraySpec> | The specification for a dynamic array to create. <ArraySpec> is one of the following: |
<idArray>[<nElements>, <nElements>, <nElements>] |
<idArray>[<nElements>][<nElements>][<nElements>] |
All dimensions except the first are optional. |
<idArray> is a valid identifier name for the array to create. Array elements are initialized to NIL. |
<nElements> defines the number of elements in a particular dimension of an array. The number of dimensions is determined by how many <nElements> arguments you specify. |
<Type> & <ClassLibrary> | The compiler recognizes the AS <Type> and the AS <Type> of <Classlibrary> clauses in the FoxPro dialect. |
PRIVATE is an executable statement, meaning it must be specified it after any variable declaration statements (such as FIELD, LOCAL, and MEMVAR) in the routine being defined.
Warning! Any reference to a variable created with this statement will produce a compiler error unless the Undeclared Variables compiler option is checked.
When you create a private variable or array, any existing and visible private or public variables with the same name are hidden until the current routine terminates or the private variable is explicitly released.
Attempting to specify a private variable that conflicts with a visible declared variable (such as LOCAL, GLOBAL, or DEFINE) with the same name is not recognized by the compiler as an error, because PRIVATE is not a compiler declaration statement. Instead, the declared variable will hide the public variable at runtime, making the public variable inaccessible until the declared variable is released.
In class methods, instance variables (except for access/assign variables) are always more visible than private variables with the same name. To access a private variable within a method when there is a name conflict, use the _MEMVAR-> alias. For access/assign variables, use the SELF: prefix to override the name conflict with a private variable.
In addition to the PRIVATE statement, you can create private variables by:
•Assigning to a variable that does not exist or is not visible, which will create a private variable.
•Receiving parameters using the PARAMETERS statement.
Private variables are dynamically scoped. They persist until the creating routine returns to its caller or until explicitly released with CLEAR ALL or CLEAR MEMORY.
Compatibility: The ALL, LIKE and EXCEPT clauses of the PRIVATE statement supported by other Xbase dialects are not supported.
The following example creates two PRIVATE arrays and three other PRIVATE variables:
PRIVATE aArray1[10], aArray2[20], var1, var2, var3
The next example creates a multi-dimensional private array using each element addressing convention:
PRIVATE aArray[10][10][10], aArray2[10, 10, 10]
This example uses PRIVATE statements to create and initialize arrays and variables:
PRIVATE aArray := { 1, 2, 3, 4 }, ;
aArray2 := ArrayNew(12, 24)
PRIVATE cChar := Space(10), cColor := SetColor()
LOCAL, MEMVAR, PARAMETERS, PUBLIC, DIMENSION, DECLARE