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()

FieldWBlock()

FIELDWBLOCK()

Return a sets/gets code block for field in a given work area

Syntax

      FIELDWBLOCK( <cFieldName>, <nWorkArea> ) --> bFieldBlock

Arguments

<cFieldName> is a string that contain the field name.

<nWorkArea> is the work area number in which <cFieldName> exist.

Returns

FIELDWBLOCK() return a code block that when evaluate could retrieve field value or assigning a new value for a field in a given work area. If <cFieldName> is not specified or from type other than character, or if <nWorkArea> is not specified or is not numeric FIELDWBLOCK() return NIL.

Description

FIELDWBLOCK() return a code block that sets/gets the value of field from a given work area. When this code block is evaluated without any parameters passed then it returns the current value of the given field. If the code block is evaluated with a parameter, than its value is used to set a new value to the field, this value is also return by the block. If the block is evaluate and there is no field with the name <cFieldName> in work area number <nWorkArea>, the code block return NIL.

Examples

      LOCAL bField
      // this block work on the field "name" that exist on work area 2
      bFiled := FIELDBLOCK( "name", 2 )
      // open a file named One in work area 1
      // that have a field named "name"
      SELECT 1
      USE one
      // open a file named Two in work area 2
      // it also have a field named "name"
      SELECT 2
      USE two
      SELECT 1
      ? "Original names: ", One->name, Two->name
      ? "Name value for file Two :", EVAL( bField )
      EVAL( bField, "Two has new name" )
      ? "and now: ", One->name, Two->name

Compliance

If the block is evaluate and there is no field with the name <cFieldName> in the given work area, the code block return NIL.

CA-Cl*pper would raise BASE/1003 error if the field does not exist.

Files

Library is rtl

Seealso

EVAL(), FIELDBLOCK(), MEMVARBLOCK()

FieldBlock()

FIELDBLOCK()

Return a code block that sets/gets a value for a given field

Syntax

      FIELDBLOCK( <cFieldName> ) --> bFieldBlock

Arguments

<cFieldName> is a string that contain the field name.

Returns

FIELDBLOCK() return a code block that when evaluate could retrieve a field value or assigning a new value to the field. If <cFieldName> is not specified or from type other than character, FIELDBLOCK() return NIL.

Description

FIELDBLOCK() return a code block that sets/gets the value of field. When this code block is evaluated without any parameters passed then it returns the current value of the given field. If the code block is evaluated with a parameter, than its value is used to set a new value to the field, this value is also return by the block. If the block is evaluate and there is no field with the name <cFieldName> in the current work area, the code block return NIL.

Note that FIELDBLOCK() works on the current work area, if you need a specific work area code block use FIELDWBLOCK() instead.

Examples

      // open a file named Test that have a field named "name"
      LOCAL bField
      bFiled := FIELDBLOCK( "name" )
      USE Test
      ? "Original value of field 'name' :", EVAL( bField )
      EVAL( bField, "Mr X new name" )
      ? "New value for the field 'name' :", EVAL( bField )

Compliance

If the block is evaluate and there is no field with the name <cFieldName> in the current work area, the code block return NIL.

CA-Cl*pper would raise BASE/1003 error if the field does not exist.

Files

Library is rtl

Seealso

EVAL(), FIELDWBLOCK(), MEMVARBLOCK()