xHarbour Reference Documentation > Statement Reference |
Creates and optionally initializes a PRIVATE memory variable.
PRIVATE <varName> [:= <xValue> ]
The PRIVATE statement creates a dynamic memory variable that has PRIVATE scope. Private variables are resolved at runtime, i.e. their symbolic name exist while a program is being executed. This makes PRIVATE variables accessible for the Macro operator (&) at the expense of execution speed. The access to LOCAL variables is faster than to PRIVATE variables.
Private variables are visible from the time of creation until the declaring routine returns, or until they are explicitely released. Visibility of PRIVATEs extends to all subroutines called after variable creation. A private variable can become temporarily hidden when a subroutine declares a variable having the same symbolic name. This can lead to subtle programming error, so that the use of PRIVATEs is discouraged unless there is good reason to use a PRIVATE variable. This is the case, for example, when it must be resolved within a macro expression.
It is possible to initialize a private variable already in the PRIVATE statement. To accomplish this, the inline-assignment operator must be used. The value of any valid expression can be assigned. This includes literal values and the return values of functions.
Note: PIVATE variables are declared with the MEMVAR statement and created with the PRIVATE statement. That means, PRIVATE is an executable statement that must follow all declaration statements.
All undeclared variables that are assigned a value with the inline assignemnt operator, are created as PRIVATE variables.
See also: | FIELD, GLOBAL, LOCAL, MEMVAR, PUBLIC, RELEASE, STATIC |
Category: | Declaration , Statements |
// These examples show different initializations of PRIVATE variables. PROCDEURE Main PRIVATE nNumber := 5 // initialized as numeric PRIVATE aUsers[10] // array with 10 empty elements PRIVATE nAge, cName // uninitialized dDate := Date() // undeclared but initialized RETURN
http://www.xHarbour.com