Skip to main content
Version: 20 R7 BETA

Value type

Value type ( expression ) : Integer

ParameterTypeDescription
expressionExpressionExpression whose resulting value to be tested
Function resultIntegerData type number

Description

The Value type command returns the type of the value resulting from the evaluation of the expression you passed as parameter.

The command returns a numeric value that can be compared with one of the following constants of the Field and Variable Types theme:

ConstantTypeValue
_o_Is floatInteger35
Is BLOBInteger30
Is BooleanInteger6
Is collectionInteger42
Is dateInteger4
Is longintInteger9
Is nullInteger255
Is objectInteger38
Is pictureInteger3
Is pointerInteger23
Is realInteger1
Is textInteger2
Is timeInteger11
Is undefinedInteger5
Is variantInteger12
Object arrayInteger39

This command is designed to return the type of a scalar expression, i.e. the value stored in or returned by the expression parameter. In particular, it can be applied to the following 4D expressions:

  • object properties (emp.name),
  • collection elements (myCol[5]).

Note: Numerical object properties are always considered real values:

 var $o : Object
 $o:=New object("value";42)
 $vType:=Value type($o.value) //$vType=Is real

Value type can be applied to any valid 4D expression, including fields, variables, and parameters. In this case, unlike the Type command, Value type returns the internal type of the value resulting from the evaluation of expression, and not its declared type. Since the 4D language converts some value types internally, the Value type result can differ from the declared type. For example, 4D internally converts the "Integer 64 bits" type field values. This provides the following results:

 $vType1:=Type([myTable]Long64field) //$vType=Is integer 64 bits
 $vType2:=Value type([myTable]Long64field) //$vType=Is real (in interpreted mode)

Other differences are related to arrays (evaluation of an array returns the current element index) and compiled mode. The following table lists these differences:

Declared typeType resultValue type result (interpreted)Value type result (compiled)Comment
ARRAY TEXT($t;1)Text arrayIs realIs longint$t contains the current element index, which is a number
Alpha fieldIs alpha fieldIs textIs text4D internally handles all strings as texts
Integer fieldIs integerIs realIs longintFor optimization reasons, in interpreted mode all numeric values are considered real and...
Long Integer fieldIs longintIs realIs longint... in compiled mode, all integer values are considered longint(*)
Integer 64 bits fieldIs integer 64 bitsIs realIs longint

(*)If you want to write a test for a numeric type value that is valid for both compiled and interpreted modes, you may consider using a code such as:

 If(Value type($myValue)=Is longint)|(Value type($myValue)=Is real)

Compatibility Note: Starting with 4D v16 R6, dates are stored in object properties either with date type or as text in ISO date format. For more information, please refer to the Dates inside objects selector of the SET DATABASE PARAMETER command.

Example 1

You want to handle the various possible types of an object property value:

 Case of
    :(Value type($o.value)=Is real)
  //handle a numeric value
    :(Value type($o.value)=Is text)
  //handle a text
    :(Value type($o.value)=Is object)
  //handle a sub-object
       ...
 End case

Example 2

You want to sum up all numeric values in a collection:

 var $col : Collection
 var $sum : Real
 $col:=New collection("Hello";20;"World2";15;50;Current date;True;10)
 For($i;0;$col.length-1) //-1 since collections start at 0
    If(Value type($col[$i])=Is real)
       $sum:=$sum+$col[$i]
    End if
 End for
 ALERT(String($sum)) //95

See also

OB Get type
Type