Variable Management Functions

Variable Management

FieldBlock Return a code block that sets/gets a value for a given field
FieldWBlock Return a sets/gets code block for field in a given work area
HB_AParams Returns an array containing values of all parameters passed to a function, method or procedure
HB_ArgC Returns the number of command line arguments
HB_ArgCheck Checks existence of an internal switch on the command line
HB_ArgShift Updates HB_Arg* parameter list removing the 1-st one and replacing it by others
HB_ArgString Retrieves the vale of an internal switch set on the command line.
HB_ArgV Retrieves the value of a command line argument
HB_PIsByRef Determine if a parameter is passed by reference.
HB_PValue Retrieves the value of an argument.
HB_ValToStr Converts any scalar type to a string.
MemVarBlock Returns a codeblock that sets/gets a value of memvar variable
PCount Retrieves the number of arguments passed to a function.
Type Retrieves the type of an expression
ValType Retrieves the data type of an expression

ValType()

ValType()

Retrieves the data type of an expression

Syntax

      ValType( <xExp> ) --> <cRetType>

Arguments

<xExp> is any valid expression.

Returns

<cRetType> a character indicating the type of the passed expression.

        <cRetType>   Meaning
        ------------ ----------------------- 
        "A"          Array
        "B"          Block
        "C"          Character (string)
        "D"          Date
        "H"          Hash
        "L"          Logical
        "M"          Memo
        "N"          Numeric
        "O"          Object
        "P"          Pointer
        "S"          Symbol
        "T"          Date & Time Stamp 
        "U"          NIL

Description

This function returns one character which represents the data type of the argument.

Examples

PROCEDURE Main()
LOCAL aStru := { { 'AINCR', '+', 4, 0 },; // Auto increment
 { 'LMODT', '=', 8, 0 },; // Last modified date & time of this record
 { 'RVERS', '^', 8, 0 },; // Row version number; modification count of this record
 { 'DT_TM', '@', 8, 0 },; // Date & Time
 { 'DOUBL', 'B', 8, 0 },; // Double Floating point / 64 bit binary
 { 'CHARA', 'C', 6, 0 },; // Character
 { 'ODATE', 'D', 8, 0 },; // Only Date 
 { 'LOGIC', 'L', 1, 0 },; // Logical
 { 'MEMOR', 'M', 8, 0 },; // Memo 
 { 'NUMER', 'N', 5, 0 },; // Numeric 
 { 'INTEG', 'I', 2, 0 },; // Integer 
 { 'OTIME', 'T', 8, 0 },; // Only Time
 { 'VARIA', 'V', 8, 0 },; // Variant 
 { 'CURRE', 'Y', 8, 0 } } // Currency 64 bit integer with implied 4 decimal

 LOCAL aArray := {},; 
 bBlock := { || .T. },; 
 cString := 'This is a string',;
 cDate := DATE(),; 
 hHash := { => },;
 lLogic := ( 1 = 1 ),;
 nNumber := 1,;
 oObject := TBrowse(),;
 pPointer := hb_idleAdd( {|| 1 + 1 } ),;
 sSymbol := ( @STR() ),; 
 uUndef := NIL,;
 xTest

 DBCREATE( 'VT_Test', aStru, , .T., 'TEST' )
 DBAPPEND()

 * Types of FIELDS:
 ? ValType( TEST->AINCR ) // N 
 ? ValType( TEST->LMODT ) // T 
 ? ValType( TEST->RVERS ) // N 
 ? ValType( TEST->DT_TM ) // T 
 ? ValType( TEST->DOUBL ) // N 
 ? ValType( TEST->CHARA ) // C 
 ? ValType( TEST->ODATE ) // D 
 ? ValType( TEST->LOGIC ) // L 
 ? ValType( TEST->MEMOR ) // M 
 ? ValType( TEST->NUMER ) // N 
 ? ValType( TEST->INTEG ) // N 
 ? ValType( TEST->OTIME ) // T 
 ? ValType( TEST->VARIA ) // C 
 ? ValType( TEST->CURRE ) // N 
 ? 
 * Types of variables assigned value from FIELDS:
 ? ValType( xTest := TEST->AINCR ) // N 
 ? ValType( xTest := TEST->LMODT ) // T 
 ? ValType( xTest := TEST->RVERS ) // N 
 ? ValType( xTest := TEST->DT_TM ) // T 
 ? ValType( xTest := TEST->DOUBL ) // N 
 ? ValType( xTest := TEST->CHARA ) // C 
 ? ValType( xTest := TEST->ODATE ) // D 
 ? ValType( xTest := TEST->LOGIC ) // L 
 ? ValType( xTest := TEST->MEMOR ) // M 
 ? ValType( xTest := TEST->NUMER ) // N 
 ? ValType( xTest := TEST->INTEG ) // N 
 ? ValType( xTest := TEST->OTIME ) // T 
 ? ValType( xTest := TEST->VARIA ) // C 
 ? ValType( xTest := TEST->CURRE ) // N 
 ? 
 * Types of Variables 
 ? ValType( aArray ) // A
 ? ValType( bBlock ) // B
 ? ValType( cString ) // C
 ? ValType( cDate ) // D
 ? ValType( hHash ) // H
 ? ValType( lLogic ) // L
 ? ValType( nNumber ) // N
 ? ValType( oObject ) // O
 ? ValType( pPointer ) // P
 ? ValType( sSymbol ) // S
 ? ValType( uUndef ) // U 

RETURN


Compliance

ValType() is CA-Cl*pper compliant, with the addition of the new Harbour types: Hash, Date & Time Stamp, Pointer and Symbol.

Files

Library is core

Seealso

Type()

Type()

Type()

Retrieves the type of an expression

Syntax

      Type( <cExp> ) --> <cRetType>

Arguments

<cExp> must be a character expression.

Returns

<cRetType> a string indicating the type of the passed expression.

        <cRetType>   Meaning

        "A"          Array
        "B"          Block
        "C"          Character (string)
        "D"          Date
        "L"          Logical
        "M"          Memo
        "N"          Numeric
        "O"          Object
        "P"          Pointer
        "S"          Symbol
        "U"          NIL, local or static variable, or not linked-in function
        "UE"         syntax error in the expression or invalid arguments
        "UI"         function with non-reserved name was requested

Description

This function returns a string which represents the data type of the argument. The argument can be any valid Harbour expression. If there is a syntax error in passed expression then “UE” is returned. If there is a call for any non-reserved Harbour function then “UI” is returned (in other words there is no call for passed UDF function during a data type determination – this is CA-Cl*pper compatible behavior). Additionally if requested user defined function is not linked into executable then “U” is returned.

The data type of expression is checked by invoking a macro compiler and by evaluation of generated code (if there is no syntax errors). This causes that Type() cannot determine a type of local or static variables – only symbols visible at runtime can be checked.

Notice the subtle difference between TYPE and VALTYPE functions. ValType() function doesn’t call a macro compiler – it simply checks the type of passed argument of any type. Type() requires a string argument with a valid Harbour expression – the data type of this expression is returned.

Examples

      ? Type( "{ 1, 2 }" )                                // prints "A"
      ? Type( "iif( .T., SubStr( "TYPE", 2, 1 ), .F. )" ) // prints "C"
      ? Type( "At( "OK", MyUDF() ) > 0" )                 // prints "UI"
      ? Type( "{ 1, 2 }[ 5 ]" )                           // prints "UE"

      //--------------------------------------------------------

      LOCAL c
      PRIVATE a := "A", b := "B"
      ? Type( "a + b + c" )     // prints: "U" ('C' variable is a local one)

      //--------------------------------------------------------

      LOCAL cFilter := Space( 60 )
      ACCEPT "Enter filter expression:" TO cFilter
      IF Type( cFilter ) $ "CDLMN"
         // this is a valid expression
         SET FILTER TO &cFilter
      ENDIF

Compliance

– Incompatibility with CA-Cl*pper: In the following code:

PRIVATE lCond := 0 ? Type( "iof( lCond, 'true', MyUDF() )" )

CA-Cl*pper will print “UE” – in Harbour the output will be “UI”

– If “UI” is returned then the syntax of the expression is correct. However invalid arguments can be passed to function/procedure that will cause runtime errors during evaluation of expression.

– Harbour supports two new types (Pointer and Symbol) which does not exists in CA-Cl*pper.

Files

Library is core

Seealso

ValType()

MemVarBlock()

MEMVARBLOCK()

Returns a codeblock that sets/gets a value of memvar variable

Syntax

      MEMVARBLOCK( <cMemvarName> ) --> <bBlock>

Arguments

<cMemvarName> – a string that contains the name of variable

Returns

<bBlock> a codeblock that sets/get the value of variable

Description

This function returns a codeblock that sets/gets the value of PRIVATE or PUBLIC variable. When this codeblock is evaluated without any parameters passed then it returns the current value of given variable. If the second parameter is passed for the codeblock evaluation then its value is used to set the new value of given variable – the passed value is also returned as a value of the codeblock evaluation.

Examples

      PROCEDURE Main()
         LOCAL cbSetGet
         PUBLIC xPublic

         cbSetGet := MEMVARBLOCK( "xPublic" )
         EVAL( cbSetGet, "new value" )
         ? "Value of xPublic variable", EVAL( cbSetGet )

         RETURN

Compliance

Clipper

Seealso

__MVGET(), __MVPUT()

Files

Library is rtl

hb_PIsByRef()

hb_PIsByRef()

Determine if a parameter is passed by reference.

Syntax

      hb_PIsByRef( nParam ) --> <lParamIsByRef>

Arguments

<nParam> is the parameter number to test.

Returns

<lVarIsByRef> a logical value indicating if the parameter is passed by reference to actual function or procedure.

Description

This function return a logical value indicating if the parameter is passed by reference to actual function or procedure.

This function is based on the form that Harbour manages to the variables for reference. When a variable is passed by reference, what receives the function or procedure is, a pointer to the previous variable, be this the container variable of the data or a pointer to another variable. The function observes if the variable passed points to a common variable or to a variable passed by reference.

Examples

      PROCEDURE Main()
         LOCAL cVar := "Test local"
         MEMVAR m_nVar
         PRIVATE m_nVar := 0

         Test( @cVar, @m_nVar, cVar, m_nVar )
         RETURN

      STATIC PROCEDURE Test( Arg1, Arg2, Arg3, Arg4 )
         ? hb_PIsByRef( 1 )        // .T.
         ? hb_PIsByRef( 2 )        // .T.
         ? hb_PIsByRef( 3 )        // .F.
         ? hb_PIsByRef( 4 )        // .F.
         RETURN

Compliance

Harbour

Files

Library is core

Seealso

ValType()

__mvXRelease()

Template

Function

Name

__mvXRelease()

Category

API

Subcategory

Variable management

Oneliner

This function releases value stored in PRIVATE or PUBLIC variable

Syntax

      __mvXRelease( <variable_name> )

Arguments

<variable_name> = either a string that contains the variable’s name or an one-dimensional array of strings with variable names No skeleton are allowed here.

Returns

Nothing

Description

This function releases values stored in memory variable. It shouldn’t be called directly, rather it should be placed into RELEASE command. If the released variable is a PRIVATE variable then previously hidden variable with the same name becomes visible after exit from the procedure where released variable was created. If you access the released variable in the same function/procedure where it was created the the NIL value is returned. You can however assign a new value to released variable without any side effects.

It releases variable even if this variable was created in different procedure

Examples

      PROCEDURE Main()
         PRIVATE mPrivate

         mPrivate := "PRIVATE from Main()"
         ? mPrivate     //PRIVATE from Main()
         Test()
         ? mPrivate     //PRIVATE from Main()

         RETURN

      PROCEDURE Main()
         PRIVATE mPrivate

         mPrivate := "PRIVATE from Main()"
         ? mPrivate           //PRIVATE from Main()
         RELEASE mPrivate
         ? mPrivate           //NIL
         mPrivate := "Again in Main()"

         RETURN

Compliance

Harbour

Files

Library is core

__mvScope()

Template

Function

Name

__mvScope()

Category

API

Subcategory

Variable management

Oneliner

If variable exists then returns its scope.

Syntax

      __mvScope( <cVarName> )

Arguments

<cVarName> = a string with a variable name to check

Returns

The symbolic values are defined in hbmemvar.ch

HB_MV_NOT_FOUND      = variable is not declared (not found in symbol table)
HB_MV_UNKNOWN        = if variable doesn't exist (but found in symbol table)
HB_MV_ERROR          = if information cannot be obtained (memory error or argument error)
HB_MV_PUBLIC         = for public variables
HB_MV_PRIVATE_GLOBAL = for private variables declared outside of current function/procedure
HB_MV_PRIVATE_LOCAL  = for private variables declared in current function/procedure

Examples

      PROCEDURE Main()
         PUBLIC mPublic
         PRIVATE mPrivateGlobal

         CallProc()
         ? __mvScope( "mPrivateLocal" )      // HB_MV_UNKNOWN

         RETURN

      PROCEDURE CallProc()
         PRIVATE mPrivateLocal

         ? __mvScope( "mPublic" )            // HB_MV_PUBLIC
         ? __mvScope( "mPrivateGlobal" )     // HB_MV_PRIVATE_GLOBAL
         ? __mvScope( "mPrivateLocal" )      // HB_MV_PRIVATE_LOCAL
         ? __mvScope( "mFindMe" )            // HB_MV_NOT_FOUND

         IF __mvScope( "mPublic" ) > HB_MV_ERROR
            ? "Variable exists"
         ELSE
            ? "Variable not created yet"
         ENDIF

         RETURN

Compliance

Harbour

Files

Library is core

Seealso

include/hbmemvar.ch

__mvRelease()

Template

Function

Name

__mvRelease()

Category

API

Subcategory

Variable management

Oneliner

This function releases PRIVATE variables

Syntax

      __mvRelease( <skeleton>, <include_exclude_flag> )

Arguments

<skeleton> = string that contains the wildcard mask for variables’ names that will be released. Supported wildcards: ‘*’ and ‘?’ <include_exclude_flag> = logical value that specifies if variables that match passed skeleton should be either included in deletion (if .T.) or excluded from deletion (if .F.)

Returns

Nothing

Description

This function releases values stored in memory variables. It shouldn’t be called directly, it should be placed into RELEASE ALL command. If the released variable is a PRIVATE variable then previously hidden variable with the same name becomes visible after exit from the procedure where released variable was created. If you access the released variable in the same function/procedure where it was created the the NIL value is returned. You can however assign a new value to released variable without any side effects. PUBLIC variables are not changed by this function.

Examples

      None Avaliable

Compliance

Harbour

Files

Library is core

__mvPut()

Template

Function

Name

__mvPut()

Category

API

Subcategory

Variable management

Oneliner

This function set the value of memory variable

Syntax

      __mvGet( <cVarName> [, <xValue>] )  --> <xValue>

Arguments

<cVarName> – string that specifies the name of variable <xValue> – a value of any type that will be set – if it is not specified then NIL is assumed

Returns

<xValue> A value assigned to the given variable.

Description

This function sets the value of PRIVATE or PUBLIC variable if this variable exists otherwise it generates a runtime error. The variable is specified by its name passed as the function parameter. If a value is not specified then the NIL is assumed

Examples

      FUNCTION MemVarBlock( cMemvar )
         RETURN {| x | ;
            iif( PCount() == 0, ;
               __mvGet( cMemvar ), ;
               __mvPut( cMemvar, x ) ) }

Compliance

Harbour

Files

Library is core

Seealso

__mvGet()

__mvPublic()

Template

Function

Name

__mvPublic()

Category

API

Subcategory

Variable management

Oneliner

This function creates a PUBLIC variable

Syntax

      __mvPublic( <variable_name> )

Arguments

<variable_name> = either a string that contains the variable’s name or an one-dimensional array of strings with variable names No skeleton are allowed here.

Returns

Nothing

Description

This function can be called either by the harbour compiler or by user. The compiler always passes the item of IT_SYMBOL type that stores the name of variable. If a variable with the same name exists already then the new variable is not created – the previous value remains unchanged. If it is first variable with this name then the variable is initialized with .T. value.

Examples

      None Avaliable

Compliance

Harbour

Files

Library is core