Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility Method to Check Object or Entity for a Desired Type
Published On: October 17, 2022

There are often times when a developer may need to work with an object, or an entity. This object, or entity, can contain many different types of data and those specific types may need to be handled differently. For example, a picture may get lost when converted to a text if not handled correctly.

Within the specified object, or entity, there may include a data type that the developer may wish to verify.

One use case of this Utility Method would be to check if an object contains a picture.

NOTE: We can check any type by passing the constant as a parameter ($2).

To check if this type exist within the object, we can use the utility method below.

Util_ObjContains($1; $2)

$1 - Object or Entity
$2 - Constant of the Field and Variable Types theme (shown below)

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

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

Here are the steps to the utility method and the code is provided below.

  • Method accepts (object or entity) and constant of field/variable type as parameter

  • Loop through object or entity

  • Utilize 4D Command Value Type to compare the value type of the current attribute to the type specified in the method parameter ($2)

  • If the types match then the method returns true

  • If $2 doesn’t match any of the value types in the object then the method returns false

  •   // Utility Method - Util_ObjContains
    #DECLARE($obj : Object; $type : Integer)->$result : Boolean
    var $att : Text

    If (Count parameters=2)
      For each ($att; $obj) Until ($result)
        If ((Value type($obj[$att]))=$type)
        End if
      End for each
    End if

    For Example:
    $hasPictureValue:=Util_ObjContains($test_o; Is picture)
    $hasRealValue:=Util_ObjContains($test2_o; Is real)