KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Creating multiple methods programmatically
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: November 8, 2018

Here is a utility method to create multiple methods names in the explorer to reduce the hassle of creating each one manually (Note this is creating blank methods with no code):

//---------------------------------------------------------------------------------
// Name: CREATE_MULTIPLE_METHODS
// Description: Method will create multiple methods programatically by default
// attributes or setting attributes with a 4D object.
//
// Parameters:
// $1 (POINTER) - Array of names for the methods
// $2 (OBJECT) - Adding attributes to the methods (optional)
// --------------------------------------------------------------------------------
C_POINTER($1;$arrProjMethodNames)
C_OBJECT($2;$methodAttrObj)
C_TEXT($methodAttr;$status)
C_LONGINT($i)
ARRAY TEXT($methodNamesArr;0)

If (Count parameters>=1)
  $arrProjMethodNames:=$1
  $methodAttr:="//%attributes = {\"lang\":\"en\"}"
  
  If (Count parameters=2)
    $methodAttrObj:=$2
    $methodAttr:="//%attributes = "+JSON Stringify($methodAttrObj)
  End if
  
  For ($i;1;Size of array($arrProjMethodNames->))
    METHOD GET NAMES($methodNamesArr)
    If (find in array($methodNamesArr;$arrProjMethodNames->{$i})=-1)
      METHOD SET CODE($arrProjMethodNames->{$i};$methodAttr)
    End if
  End for
End if


Example #1: Creating some methods

ARRAY TEXT($mNamesArr;0)

APPEND TO ARRAY($mNamesArr;"setWidth")
APPEND TO ARRAY($mNamesArr;"getWidth")
APPEND TO ARRAY($mNamesArr;"setHeight")
APPEND TO ARRAY($mNamesArr;"getHeight")
APPEND TO ARRAY($mNamesArr;"toggleView")

CREATE_MULTIPLE_METHODS (->$mNamesArr)


The explorer window will populate as the following:




Example #2: Creating same methods with attributes

// Using the same array code from Example #1
C_OBJECT($obj)
OB SET($obj;"lang";"en";"shared";True)

CREATE_MULTIPLE_METHODS (->$mNamesArr;$obj)


Each of the methods created will have the "shared component setting":



Keep in mind that the object can be built also by these attributes: