Show/Hide Toolbars

XSharp

NoteThis command is not available in the Core and Vulcan dialects

Purpose

Create variables and arrays visible within current and invoked routines.

Syntax

PRIVATE <idVar> [:= <uValue>] | <ArraySpec> [, ...]
PRIVATE <idVar> [:= <uValue>] [AS <Type> [OF <ClassLibrary>] ]  // FoxPro dialect

Arguments

<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.

Description

PRIVATE is an executable statement which means you must specify it after any variable declaration statements (such as FIELD, LOCAL, and MEMVAR) in the routine that you are defining.

 

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, existing and visible private and public variables of 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 (for example, LOCAL, GLOBAL, or DEFINE) of 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.  This means that you will not be able to access the public variable at all until the declared variable is released.

 

In class methods, instance variables (with the exception of access/assign variables) are always more visible than private variables of the same name.  Use the _MEMVAR-> alias to access a private variable within a method if there is a name conflict.  For access/assign variables, use the SELF:  prefix to override a 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 will create a private variable

Receiving parameters using the PARAMETERS statement

 

Private variables are dynamically scoped.  They exist until the creating routine returns to its caller or until explicitly released with CLEAR ALL, or CLEAR MEMORY.  

Notes

Compatibility: The ALL, LIKE, and EXCEPT clauses of the PRIVATE statement supported by other Xbase dialects are not supported.

Examples

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()

See Also

LOCAL, MEMVAR, PARAMETERS, PUBLIC, DIMENSION, DECLARE