The following statements demonstrate that TYPE( ) expects a character string containing an expression:

dVar = date( ) // Create a date variable

? type( dVar ) // Error: Data type mismatch. Expecting: Character

? type( "dVar" ) // Displays "D" for Date, as do the next two statements

? type( "D" + "var" ) // Any expression containing the variable name works

// (and variable names are not case-sensitive) 

? type( "date( )" ) // String can contain any expression, not just single variable

The following routine is used to read the data in a generated text file into the corresponding fields of a table. Character fields in the text file are the same length as in the table. Dates are formatted in six characters as MMDDYY (which matches the current SET DATE format). Numbers are always twelve characters and represent currency stored in cents, so it needs to be divided by 100.

function decodeLine( cLine, aDest )

   #define YEAR_LEN 2 

   #define NUM_LEN 12 

   local nPtr, nFld, cFld, nLen 

   nPtr = 1 // Pointer into string 

   for nFld = 1 to fldcount( ) 

      cFld = field( nFld ) // Store name of field in string variable for reuse 

      do case 

      case type( cFld ) == "C" 

         aDest[ nFld ] = substr( cLine, nPtr, flength( nFld )) 

         nPtr += flength( nFld ) 

      case type( cFld ) == "D" 

         aDest[ nFld ] = ctod( substr( cLine, nPtr, 2 ) + "/" + ; 

         substr( cLine, nPtr + 2, 2 ) + "/" + ; 

         substr( cLine, nPtr + 4, YEAR_LEN ) ) 

         nPtr += 2 + 2 + YEAR_LEN 

      case type( cFld ) == "N" 

         aDest[ nFld ] = val( substr( cLine, nPtr, NUM_LEN )) / 100 

         nPtr += NUM_LEN 

      endcase 

   endfor 

An array is passed to the routine along with the line to read. The field values are stored in the array, which is appended to the table with APPEND FROM ARRAY in the calling routine (not shown here). The function defines some manifest constants for the size of a numeric field and whether the year is two or four digits in case this changes in the future. A FOR loop goes through each field in the table. The name of each field is stored in a variable for convenience; it’s used repeatedly in the DO CASE structure. The variable is a string containing the name of the field, which TYPE( ) expects. In contrast, TYPE("cFLD") would always return "C".

The following function is a slight variation on the TYPE( ) function. It returns the type of a value, but it expects the expression itself as a parameter instead of a string containing the expression. It therefore cannot handle undefined or invalid expressions, but it can handle local and static variables. It also returns the character "0" (zero) when the expression contains the value null, to differentiate it from expressions that have no value. For example, if you have a function that does not RETURN a value, that function call has an undefined value.

function valType

   parameter x 

 return iif( x == null, "0", type( "x" ) ) 

The function works by taking the parameter as a private variable named x. Then if it’s not null, the TYPE( ) function is used with the string "x" to return the type of the parameter.