PUBLIC

PUBLIC

Create and initialize public memory variables and arrays

Syntax

       PUBLIC <identifier> [[:= <initializer>], ... ]

Arguments

<identifier> is the name of a public variable or array to create. If the <identifier> is followed by square brackets ([ ]), it is created as an array. If the <identifier> is an array, the syntax for specifying the number of elements for each dimension can be array[<nElements>, <nElements2>, …] or array[<nElements>][<nElements2>]…. The maximum number of elements per dimension is 4096. The maximum number of dimensions per array is limited only by available memory.

<initializer> is the optional assignment of a value to a new public variable. Array identifiers, however, cannot be given values with an <initializer>. An <initializer> for a public variable consists of the inline assignment operator (:=) followed by any valid Harbour expression including a literal array. Except for arrays, if no <initializer> is specified, public variables are initialized to false (.F.). This is an exception to the general rule that uninitialized variables are NIL. With arrays, however, the initial value of each element is NIL.

A list of variables and arrays can be created and, optionally, initialized with one PUBLIC statement if each definition is separated by a comma.

Description

The PUBLIC statement creates variables and arrays visible to all procedures and user-defined functions in a program. Public variables exist for the duration of the program or until explicitly released with CLEAR ALL, CLEAR MEMORY, or RELEASE. Declaring private, local, or static variables or arrays with the same name as existing public variables temporarily hides those public variables until the overriding variables are released or are no longer visible. An attempt to create a public variable with the same name as an existing and visible private variable is simply ignored (see Notes below for an exception).

Attempting to specify a PUBLIC variable that conflicts with a previous FIELD, LOCAL, or STATIC declaration of the same name results in a fatal compiler error. This is true regardless of the scope of the declaration.

PUBLIC statements are executable statements and, therefore, must be specified within the body of a procedure or user-defined function definition. They also must follow all compile-time declarations, such as FIELD, LOCAL, MEMVAR, and STATIC.

The maximum number of public and private variables and arrays that can simultaneously exist in a single program is 2048.

For more information on variable declarations and scoping, refer to the Variables section in the “Basic Concepts” chapter.

Notes

. Public array name conflicts with existing private variables: The statement, PUBLIC x[10], will not create the public array x if there is already a private or public variable x. It will, however, destroy the contents of the existing x, replacing it with a reference to a ten-element array.

Examples

       .  This example creates two PUBLIC arrays and one PUBLIC
          variable:
          PUBLIC aArray1[10, 10], var2
          PUBLIC aArray2[20][10]
       .  The following PUBLIC statements create variables and
          initialize them with values:
          PUBLIC cString := SPACE(10), cColor := SETCOLOR()
          PUBLIC aArray := {1, 2, 3}, aArray2 := ARRAY(12, 24)

Seealso

LOCAL, MEMVAR, PARAMETERS, PRIVATE, STATIC

PRIVATE

Create and initialize private memory variables and arrays

Syntax

       PRIVATE <identifier> [[:= <initializer>], ... ]

Arguments

<identifier> is the name of a private variable or array to create. If the <identifier> is followed by square brackets ([ ]), an array is created and assigned to the <identifier>. When the <identifier> specification indicates an array, the syntax for specifying the number of elements for each dimension can be array[<nElements>, <nElements2>, …] or array[<nElements>][<nElements2>]… The maximum number of elements per dimension is 4096. The maximum number of dimensions is limited only by available memory.

<initializer> is the optional assignment of a value to a new private variable. An array cannot be given values with an <initializer>. An <initializer> for a private variable consists of the inline assignment operator (:=) followed by any valid Harbour expression including a literal array. If no explicit <initializer> is specified, the variable is initialized to NIL. In the case of an array, each element is initialized to NIL.

You can create and, optionally, initialize a list of variables and arrays with one PRIVATE statement if the definitions are separated by commas.

Description

The PRIVATE statement creates variables and arrays visible within the current and invoked procedures or user-defined functions. This class of variable is said to have dynamic scope. Private variables exist for the duration of the active procedure or until explicitly released with CLEAR ALL, CLEAR MEMORY, or RELEASE. When a private variable or array is created, existing and visible private and public variables of the same name are hidden until the current procedure or user-defined function terminates.

Attempting to specify a PRIVATE variable that conflicts with a previous FIELD, LOCAL, or STATIC declaration of the same name results in a fatal compiler error. This is true regardless of the scope of the declaration.

PRIVATE statements are executable statements and, therefore, must be specified within the body of a procedure or user-defined function and must follow all variable declarations, such as FIELD, LOCAL, MEMVAR, and STATIC.

In addition to the PRIVATE statement, private variables are also created in two other ways:

. Assignment to a variable that does not exist or is not visible will create a private variable .

Parameters received using the PARAMETERS statement are created as private variables with the same lifetime and visibility.

No more than 2048 private and public variables and arrays can simultaneously exist in a single program.

For more information on variable declarations and scoping, refer to the Variables section in the “Basic Concepts” chapter.

Notes

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

Examples

       .  This example creates two PRIVATE arrays and three other
          PRIVATE variables:
          PRIVATE aArray1[10], aArray2[20], var1, var2, var3
       .  This example creates a multidimensional 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 := ARRAY(12, 24)
          PRIVATE cChar := SPACE(10), cColor := SETCOLOR()

Seealso

FIELD, LOCAL, MEMVAR, PARAMETERS, PUBLIC, STATIC

__mvExist()

Template

Function

Name

__mvExist()

Category

API

Subcategory

Variable management

Oneliner

Determine if a given name is a PUBLIC or PRIVATE memory variable

Syntax

      __mvExist( <cVarName> )  --> <lVariableExist>

Arguments

<cVarName> – string that specifies the name of variable to check

Returns

__mvExist() return TRUE (.T.) if a MEMVAR named <cVarName> exist.

Description

This function determine if a PUBLIC or PRIVATE variable with the name <cVarName> exist or not.

Examples

      LOCAL   TheLocal
      STATIC  TheStatic
      PUBLIC  ThePublic
      PRIVATE ThePrivate
      ? __mvExist( "NotExist"   )        // .F.
      ? __mvExist( "TheLocal"   )        // .F.
      ? __mvExist( "TheStatic"  )        // .F.
      ? __mvExist( "ThePublic"  )        // .T.
      ? __mvExist( "ThePrivate" )        // .T.

Compliance

Harbour

Seealso

MEMVAR, PRIVATE, PUBLIC

Files

Library is core

MEMVAR

MEMVAR

Declares private and public variables and arrays.

Syntax

      MEMVAR <xVar>

Arguments

<xVar> Memory variable Name

Description

This command tells the compiler to resolve any reference to a memory variable designated within this list s if it possessed an explicit memory variable alias with either the M-> or MEMVAR-> prefix.Only those memory variables that do not contain any such explicit are affected by this command. Those memory variabls within macro expansions are not affected by this command.

The MEMVAR declaration must apear before any executable commands;it is similat to the LOCAL, STATIC, FIELD, PARAMETERS, FUNCTION, and PROCEDURE commands statements.

Examples

      MEMVAR y AS NUMERIC
      PROCEDURE Main()
         LOCAL n, lVar

         n := iif( lVar, "A", 3 )
         n := 2
         n := "a"
         n := seconds() + 2
         n := int( seconds() + 2 )
         y := n
         ? y
         RETURN

Tests

      See tests/testwarn.prg for more examples

Compliance

Clipper

Platforms

All

Files

None.

Seealso

LOCAL, STATIC, FIELD, PRIVATE, PUBLIC

LOCAL

LOCAL

Initializes a local memory variable or array

Syntax

      LOCAL <xVar> [:= <xInit> ]

Arguments

<xVar> Name of a memory variable or array.

<xInit> Value to be assinged to a variable or array

Description

This command created a LOCAL memory variable or array. The name of either is specified in <xVar>. If more then one variable is being initialized with the LOCAL command, separate each entry with a comma. If a variable or an array is to be assingned a start-up value, that expression may be specified in <xInit> and folling. Is Strong type compile mode is used, the Compiler will check if the value recived matchs the type specified in <xType>.

LOCAL varibles are symbols generated at run time and are resolved at compile time. The visibility and life span of a LOCAL variable or array is limited to the function or procedure in which it is defined.

No macro expansions are allowed in the LOCAL declaration statement.

No Harbour command other then FUNCTION, PROCEDURE, PUBLIC, PRIVATE, PARAMETERS, MEMVAR, STATIC and FIELD, may precede the LOCAL command.

LOCAL array reference may not be initialized (i.e., assigned values) on the same command line as the LOCAL command statement. This can be done later in the program.

LOCAL variables and arrays are not affected by the RELEASE command.

Examples

      PROCEDURE Main()
         LOCAL n, lVar

         n := iif( lVar, "A", 3 )
         n := 2
         n := "a"
         n := seconds() + 2
         n := int( seconds() + 2 )
         RETURN

Tests

      See tests/testwarn.prg for more examples

Compliance

Clipper

Platforms

All

Files

None

Seealso

FIELD, PRIVATE, PUBLIC, STATIC, MEMVAR

FIELD

FIELD

Declares a list of database field names.

Syntax

      FIELD <xField> [,<xFieldn...>  [in <cDatabase>]

Arguments

<xField> A valid field name

<xFieldn> Additional field name

<cDatabase> An valid alias name

Description

This command declares the names of fields <xField> (and <xFieldn> and following) with an optional alias identifier as <cDatabase> for each. This command allow Harbour to resolve any reference to a field specified in the field list by viewing it as a field when it is not referenced by an alias. If a field is not listed in this list and it is not explicity tagged with an alias indentifier, it may be viewed as a memory variable, which may cause run-time errors. This command has no effect on memory variables or on field reference buried within a macro expression.

Examples

      PROCEDURE Main()
         FIELD Id
         FIELD Name
         USE tests NEW
         Name := "Sales"
         Id := 5
         USE
         RETURN

Tests

      See tests/testwarn.prg

Compliance

Clipper

Platforms

All

Files

None.

Seealso

MEMVAR, PRIVATE, PUBLIC, STATIC

C5_MEMVAR

MEMVAR
 Declare private and public variable names
------------------------------------------------------------------------------
 Syntax

     MEMVAR <idMemvar list>

 Arguments

     <idMemvar list> is a list of public and private variable names to
     declare to the compiler.

 Description

     MEMVAR is a declaration statement that causes the compiler to resolve
     references to variables specified without an explicit alias by
     implicitly assuming the memory variable alias (MEMVAR->).  Only
     explicit, unaliased references to the specified variables are affected.
     MEMVAR, like all declaration statements, has no effect on references
     made within macro expressions or variables.

     The MEMVAR statement neither creates the variables nor verifies their
     existence.  Its primary effect is to ensure correct references to
     variables whose existence is known to be guaranteed at runtime.  At
     runtime, the specified variables must be created using the PRIVATE,
     PARAMETERS or PUBLIC statements.  This can occur in the procedure
     containing the MEMVAR declaration or in a higher -level procedure.
     Attempting to access the variables before they are created will cause an
     error.

     The scope of the MEMVAR declaration is the procedure or function in
     which it occurs, or the entire source file if it precedes any PROCEDURE
     or FUNCTION statements and the /N compiler option is used.  The /N
     option suppresses automatic definition of a procedure with the same name
     as the program (.prg) file.

     Like other declaration statements, MEMVAR must precede any executable
     statements, including PARAMETERS, PUBLIC, and PRIVATE statements in a
     procedure or function definition, or the program (.prg) file if the
     declaration has filewide scope.

     MEMVAR can be used in conjunction with the /W compiler option--which
     generates warning messages for ambiguous variable references--to perform
     compile--time checking for undeclared variables.

     For more information on variable declarations and scoping, refer to the
     Variables section in the "Basic Concepts" chapter of the Programming and
     Utilities Guide.

 Examples

     .  This example demonstrates the relationship between a private
        and field variable with the same name.  The private variable is
        declared with the MEMVAR statement:

        FUNCTION Example
           MEMVAR amount, address
           PRIVATE amount := 100
           USE Customer NEW
           //
           ? amount                // Refers to amount private variable
           ? Customer->Amount      // Refers to Amount field variable
           //
           RETURN NIL

See Also: FIELD LOCAL PRIVATE PUBLIC STATIC



-> Alias assignment

->
 Alias assignment--binary                        (Special)
------------------------------------------------------------------------------
 Syntax

     <idAlias>-><idField>
     <idAlias>->(<exp>)
     (<nWorkArea>)-><idField>
     (<nWorkArea>)->(<exp>)
     FIELD-><idVar>
     MEMVAR-><idVar>

 Operands

     <idAlias> is the name of the unselected work area to access and must
     refer to a work area with a database file in USE.

     <nWorkArea> is the number of the unselected work area to access.

     <idField> is the name of a field in the specified work area.

     <exp> is an expression of any data type to be executed in the
     specified work area.  If an expression more complicated than a single
     field reference is used as the second operand, the expression must be
     enclosed in parentheses ( ).

     <idVar> is any valid CA-Clipper identifier.  Depending on whether
     you specify FIELD or MEMVAR, the identifier will be forced to a database
     field or memory variable (public or private) reference.

 Description

 When used with an <idAlias> as the first operand, the alias operator

     (->) accesses field information or evaluates an expression in the
     indicated work area.  The alias operator implicitly SELECTs the
     <idAlias> before evaluating the <idField> or <exp> operand.  When the
     evaluation is complete, the original work area is SELECTed again.  An
     alias reference can be in an expression or on a line by itself:

     ? Customer->Name
     Customer->(UpdateTransaction())

     Using the alias operator lets you:

     .  Access information from unselected work areas within
        expressions

     .  Access environmental information from unselected work areas

     .  Access information from unselected work areas in modes such as
        REPORT and LABEL FORMs

     .  Write more compact code

     In addition to allowing expression and field evaluation in unselected
     work areas, the alias operator makes an explicit reference to a field or
     variable using either the FIELD or the MEMVAR keyword aliases.  MEMVAR
     forces <idVar> to refer to a memory variable name, and FIELD forces it
     to reference a database field.  These special alias identifiers allow
     you to avoid ambiguity when there are conflicts between field and memory
     variable names.  Remember that a reference to a variable identifier not
     prefaced with an alias defaults to a field if there are both field and
     memory variables with the same name.  To override this, use the (/V)
     option when compiling.

     In addition to specifying the alias as an identifier, you can access the
     target work area using an expression that returns the work area number
     if the expression is enclosed by parentheses.  This lets you use work
     area numbers as handles, which is useful for passing references to work
     areas without using macros, aliases, names, etc.

 Examples

     .  This example accesses database and work area information in an
        unselected work area:

        USE Customer NEW
        USE Invoices NEW
        ? Customer->CustName                  // Result: Bill Smith
        ? Customer->(RECNO())                 // Result: 1
        ? Customer->(FOUND())                 // Result: .F.
        ? Customer->(City + ", " + State + ;
           "  " + Zip)                        // Result: ShadowVille,
                                              //         CA  90415

     .  This example uses a user-defined function (MySeek()) as an
        operand of the alias operator for a common operation that normally
        requires many more statements:

        IF Invoices->(MySeek(CustNum))
           <process customer>...
        ELSE
           <process no find>...
        ENDIF
        RETURN

        FUNCTION MySeek( cSearch )
           SEEK cSearch
        RETURN (FOUND())

        Note:  This example is just an illustration of the alias operator
        with a user-defined function.  CA-Clipper's DBSEEK() could be used
        instead of MySeek().

     .  This example explicitly references field and memory variables
        with the same name:

        USE Customer NEW
        MEMVAR->CustName = "Bill Smith"      // Create a memvar
                                             // CustName
        LOCATE FOR MEMVAR->CustName = FIELD->CustName

     .  This example uses an expression as a work area handle to
        create a work area-independent database operation:

        cTable1 := "C:Myfile.dbf"
        cTable2 := "D:Myfile.dbf"
        USE (cTable1) NEW
        hArea1 = SELECT()
        USE (cTable2) NEW
        hArea2 = SELECT()
        DoStuff( hArea1, hArea2 )

        FUNCTION DoStuff( hArea1, hArea2 )
           LOCAL nCount, nSave
           nSave := SELECT()
           SELECT (hArea1)
           FOR nCount := 1 TO FCOUNT()
              FIELDPUT( nCount, ( hArea2 )-> ;
                 ( FIELDGET( nCount )))
           NEXT
           SELECT (nSave)
           RETURN NIL

See Also: FIELD FIELDNAME() FIELDPOS() FIELDGET() SELECT

The Clipper conversion process

The Clipper conversion process (.pdf)

Try these new 5.01 language constructs

Try these new 5.01 language constructs