xHarbour Reference Documentation > Statement Reference |
Creates and optionally initializes a PUBLIC memory variable.
PUBLIC <varName> [:= <xValue> ]
When a PUBLIC variable is not assigned a value upon creation, it is initialized with .F. (false). This is different to all other variable types, which are initialized with NIL, by default.
The PUBLIC statement creates a dynamic memory variable that has PUBLIC scope. Public variables are resolved at runtime, i.e. their symbolic name exist while a program is being executed. This makes PUBLIC variables accessible for the Macro operator (&) at the expense of execution speed. The access to GLOBAL variables is faster than to PUBLIC variables.
Public variables are visible from the time of creation until they are explicitely released. Visibility of PUBLICs extends to the entire program, even if a PUBLIC variable is created in a sub-routine which has returned. A public variable can become temporarily hidden when a subroutine declares a variable having the same symbolic name.
It is possible to initialize a public variable already in the PUBLIC 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: PUBLIC variables are declared with the MEMVAR statement and created with the PUBLIC statement. That means, PUBLIC is an executable statement that must follow all declaration statements.
See also: | FIELD, GLOBAL, LOCAL, MEMVAR, PRIVATE, RELEASE, STATIC |
Category: | Declaration , Statements |
// This example creates PUBLIC variables in a sub-routine called from // the Main routine PROCEDURE Main ? Type( "dataPath" ) // result: U ? Type( "tempPath" ) // result: U MakeConfig() ? dataPath // result: C:\xhb\apps\data ? tempPath // result: C:\xhb\apps\temp RETURN PROCEDURE MakeConfig() PUBLIC dataPath := "C:\xhb\apps\data" PUBLIC tempPath := "C:\xhb\apps\temp" RETURN
http://www.xHarbour.com