KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: The CHOOSE Command
PRODUCT: 4D Developer | VERSION: 11 | PLATFORM: Mac & Win
Published On: November 19, 2007

4D v11 SQL now has a new command named CHOOSE.

The CHOOSE command is a replacement command for CASE Statements and IF conditionals. It will return the appropriate values based upon the criteria you specify. The CHOOSE command accepts booleans and numbers as criteria. It reduces the size of these statements to a single line.

-If a Boolean, Choose returns value1 if True and value2 if False. In this case, the command expects exactly three parameters: criterion, value1 and value2.

-If an integer, Choose returns the value whose position corresponds to criterion. Be careful, numbering of the values begins with 0 (the position of value1 is thus 0). In this case, the command expects at least two parameters: criterion and value1.

An example:

vJob:=Choose([Person]Job;"CEO";"Software Engineer";"Starbucks Barista";"Comedian")


This code is exactly equivalent to:

Case of
 :([Person]Job=0)
   vJob:="CEO"
 :([Person]Job=1)
   vJob:="Software Engineer"
 :([Person]Job=2)
   vJob:="Starbucks Barista"
 :([Person]Job=3)
   vJob:="Comedian"
End case