KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Returning Multiple Data Types from a Single Method
PRODUCT: 4D | VERSION: 14.0 | PLATFORM: Mac & Win
Published On: August 11, 2014

4D v14 introduces a new data type, Objects, which can be declared with C_OBJECT. Using objects developers can now return multiple types from a single method.

For example the following method will be called SampleObject():

C_OBJECT($0,$temp_Obj)

OB SET($temp_Obj;"int";5)
OB SET($temp_Obj;"text";"Hi ")
OB SET($temp_Obj;"bool";True)

$0 := $temp_Obj


SampleObject() takes no parameters, but returns three different data types encapsulated in an object. To change or add data to an object, the command, OB SET needs to be used. Afterwards, the command, OB GET can be used to extract data from the object and use it. Setters and Getters are the fundamentals of objects. SampleObject() can then be used as follows:

C_OBJECT($Object)

$Object:=SampleObject()

If (OB GET($Object;"bool"))
   ALERT(OB GET($Object;"text") * OB GET($Object;"int"))
End if


Because “bool” in $Object was True, the if block is entered. Next ALERT is called and generates the message in $Object’s “text,” which is “Hi, “ and multiplies it by $Object’s “int,” which is 5. The method generates the following output:



From one single method being called in a single line, three different data types were able to be returned: a Longint, a Text, and a Bool. This method of using objects to return multiple data types from a single method can be beneficial in simplifying and reducing the size of codes. Records usually contain more than one type of data, a method could be used to return all of the data types in a single object for other uses. These methods can also be applied to Array Objects to return an Array of Objects.