Skip to main content
Version: Next

Type

Type ( fieldVar ) -> Function result

ParameterTypeDescription
fieldVarField, Variable🡒field or variable to be tested
Function resultLongint🡐Data type number

Description

The Type command returns a numeric value that indicates the type of field or variable you have passed in the fieldVar parameter.

4D provides the following predefined constants found in the Field and Variable Types theme:

ConstantTypeValue
Array 2DLongint13
Blob arrayLongint31
Boolean arrayLongint22
Date arrayLongint17
Integer arrayLongint15
Is alpha fieldLongint0
Is BLOBLongint30
Is BooleanLongint6
Is collectionLongint42
Is dateLongint4
Is integerLongint8
Is integer 64 bitsLongint25
Is longintLongint9
Is nullLongint255
Is objectLongint38
Is pictureLongint3
Is pointerLongint23
Is realLongint1
Is string varLongint24
Is subtableLongint7
Is textLongint2
Is timeLongint11
Is undefinedLongint5
Is variantLongint12
LongInt arrayLongint16
Object arrayLongint39
Picture arrayLongint19
Pointer arrayLongint20
Real arrayLongint14
String arrayLongint21
Text arrayLongint18
Time arrayLongint32

You can apply the Type function to fields, interprocess variables, process variables, local variables, and dereferenced pointers for these types of objects. You can apply Type to the parameters ($1, $2 ... ${...}) of a project method or to the result of a function ($0).

Notes:

  • You can not apply the Type function to scalar expressions such as object properties (emp.name) or collection elements (myColl[5]). To do this, you must use the Value type command.
  • In compiled mode, calling Type on a method parameter ($0, $1...) declared as C_VARIANT does not return Is variant but the actual data type (same as calling Value type).

Example 1

The following project method empties some or all of the fields for the current record of the table whose a pointer is passed as parameter. It does this without deleting or changing the current record:

  // EMPTY RECORD Project Method
  // EMPTY RECORD ( Pointer {; Long } )
  // EMPTY RECORD ( -> [Table] { ; Type Flags } )
 
 var $1 : Pointer
 var $2;$vlTypeFlags : Integer
 
 If(Count parameters>=2)
    $vlTypeFlags:=$2
 Else
    $vlTypeFlags:=0xFFFFFFFF
 End if
 For($vlField;1;Get last field number($1))
    $vpField:=Field(Table($1);$vlField)
    $vlFieldType:=Type($vpField->)
    If($vlTypeFlags ??$vlFieldType )
       Case of
          :(($vlFieldType=Is alpha field)|($vlFieldType=Is text))
             $vpField->:=""
          :(($vlFieldType=Is real)|($vlFieldType=Is integer)|($vlFieldType=Is longint))
             $vpField->:=0
          :($vlFieldType=Is date)
             $vpField->:=!00/00/00!
          :($vlFieldType=Is time)
             $vpField->:=?00:00:00?
          :($vlFieldType=Is Boolean)
             $vpField->:=False
          :($vlFieldType=Is picture)
             var $vgEmptyPicture : Picture
             $vpField->:=$vgEmptyPicture
          :($vlFieldType=Is subtable)
             Repeat
                ALL SUBRECORDS($vpField->)
                DELETE SUBRECORD($vpField->)
             Until(Records in subselection($vpField->)=0)
          :($vlFieldType=Is BLOB)
             SET BLOB SIZE($vpField->;0)
       End case
    End if
 End for

After this project method is implemented in your database, you can write:

  // Empty the whole current record of the table [Things To Do]
 EMPTY RECORD(->[Things To Do])
 
  // Empty Text, BLOB and Picture fields for the current record of the table [Things To Do]
 EMPTY RECORD(->[Things To Do];0?+Is text?+Is BLOB?+Is picture)
 
  // Empty the whole current record of the table [Things To Do] except Alphanumeric fields
 EMPTY RECORD(->[Things To Do];-1?-Is alpha field)

Example 2

In certain cases, for example when writing generic code, you may need to find out whether an array is a standard independent array or the “row” of a 2D array. In this case, you can use the following code:

 ptrmyArr:=->myArr{6} // Is myArr{6} the row of a 2D array?
 RESOLVE POINTER(ptrmyArr;varName;tableNum;fieldNum)
 If(varName#"")
    $ptr:=Get pointer(varName)
    $thetype:=Type($ptr->)
  // If myArr{6} is the row of a 2D array, $thetype equals 13
 End if

Example 3

See example for the APPEND DATA TO PASTEBOARD command.

See also

Is a variable
Undefined
Value type