KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A utility to empty and initialize one or more arrays
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: March 13, 2013

It may become necessary when updating a 4D Form page to return an array object back to its initial creation state. There is more to that task than just removing all the elements in the target array.

Since an array alway has one array item, item 0, it is a common practice to store data in that element, knowing it will never be presented on screen. It is also a common occurrence for an integer value to be stored in the array variable itself. This value is zero when an array is first declared regardless of the type of array it is.

The utility below returns one or more arrays to their originally initialized state.

If (True)
      If (False)
         Begin SQL
         /*
          UTIL_EmptyAndInitArray

          Purpose: Empty and initialize one or more arrays

          $1 - $n - Pointer - Pointer to arrays
         */
         End SQL
      End if
      C_TEXT($MethodName_T)
      $MethodName_T:=Current method name
       //===================== Declare Variables ==================================
       //method_parameters_declarations
      C_POINTER(${1})
       //---------------------------------------------------------------------------
       //method_wide_constants_declarations
       //---------------------------------------------------------------------------
       //local_variable_declarations
      C_LONGINT($Ndx;$SOA;$RIS;$Params_L;$Size_L;$Type_L)
      C_POINTER($Array_P;$Nil_P)

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

$Params_L:=Count parameters
If ($Params_L>0)
       //======================== Method Actions ==================================

      For ($Ndx;1;$Params_L;1)

         $Array_P:=${$Ndx}

          // Empty the array
          // (
         $Size_L:=Size of array($Array_P->)
         If ($Size_L>0)
              DELETE FROM ARRAY($Array_P->;1;$Size_L)
         End if
          // )

          // Clear any value from the array var itself
          // (
         $Array_P->:=0
          // )

          // Init the array
          // (
         $Type_L:=Type($Array_P->)
          //
          // Clear any possible entries into array item #0 which always exists
          // simply because the array has been declared
          //
         Case of
             //______________________________________________________
            : ($Type_L=Integer array) | ($Type_L=LongInt array) | ($Type_L=Real array)
               $Array_P->{0}:=0
             //______________________________________________________
            : ($Type_L=String array) | ($Type_L=Text array)
               $Array_P->{0}:=""
             //______________________________________________________
            : ($Type_L=Boolean array)
               $Array_P->{0}:=False
             //______________________________________________________
            : ($Type_L=Date array)
               $Array_P->{0}:=!00/00/00!
             //______________________________________________________
            : ($Type_L=Picture array)
               $Array_P->{0}:=$Array_P->{0}*0
             //______________________________________________________
            : ($Type_L=Pointer array)
               $Array_P->{0}:=$Nil_P
             //______________________________________________________
         End case
          // )

      End for

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

End if