KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility that returns the number of Key-Value pairs in a 4D v14 Object
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: February 20, 2014

4D v14 Objects (C_OBJECT) are 4D's equivilant of an JSON object in Javascript. If working in Javascript then number of key-value pairs in the object would be aquired from the Object "length" property like this...

var count = Object.keys( myObj ).length;

Such a propetty or function does not yet exist in the v14 language, but the number is easily derived.

The utility below provides returns the number of "key-value' pairs in a 4D v14 Object.

If (True)
   If (False)
      Begin SQL
         /*
          Name: OBJECT_Length
          Path: OBJECT_Length

          Purpose: Return the number of "Key-Value" pairs in a C_OBJECT instance

          $1 - POINTER - Pointer to the C_OBJECT reference
         */
      End SQL
   End if
   C_TEXT($MethodName_T)
   $MethodName_T:=Current method name
    //===================== Declare Variables ==================================
    //method_parameters_declarations
   C_LONGINT($0)
   C_POINTER($Obj_P;$1)
    //---------------------------------------------------------------------------
    //method_wide_constants_declarations
    //---------------------------------------------------------------------------
    //local_variable_declarations
   C_LONGINT($Ndx;$SOA;$RIS;$Params_L)

End if
//====================== Initialize and Setup ================================

$Params_L:=Count parameters
If ($Params_L=1)

    //======================== Method Actions ==================================

   $Obj_P:=$1
   ARRAY TEXT($Names_aT;0)
   OB GET PROPERTY NAMES($Obj_P->;$Names_aT)
   $SOA:=Size of array($Names_aT)

    //======================== Clean up and Exit =================================

End if

$0:=$SOA



Example...
C_OBJECT($Object)
OB SET($Object;"LastName";"James";"age";35)
OB SET($Object;"FirstName";"Jesse")

$Len_L:=OBJECT_Length (->$Object)
// $Len_L is 3