xHarbour Reference Documentation > Statement Reference xHarbour Developers Network  

PUBLIC

Creates and optionally initializes a PUBLIC memory variable.

Syntax

PUBLIC <varName> [:= <xValue> ]

Arguments

PUBLIC <varName>
<varName> is the symbolic name of the PUBLIC variable to createe.
<xValue>
<xValue> is an optional value to assign to the PUBLIC variable after being created. To assign a value, the inline assignment operator (:=) must be used. The simple assignment operator (=) cannot be used.

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.

Description

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.

Info

See also:FIELD, GLOBAL, LOCAL, MEMVAR, PRIVATE, RELEASE, STATIC
Category: Declaration , Statements

Example

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

Copyright © 2006-2007 xHarbour.com Inc. All rights reserved.
http://www.xHarbour.com
Created by docmaker.exe