KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Implementing hierarchical tests using "Case of... Else... End" case statements
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: March 17, 2000

When you want to implement hierarchical tests using the "Case of... Else... End" case statements, you need to keep in mind the way that statement type operates. The "Case of" statement is sequential in nature and tests the conditions in the order defined in the method editor. For simple tests, as described in the example below, the test sequence is not relevant:

Case of

: (vResult = 1) ` Test if the number is 1
` Do something
: (vResult = 2) ` Test if the number is 2
` Do something else
: (vResult = 3) ` Test if the number is 3
` Do... well...something different

End case

This example illustrates a proper use of the case of statement since vResult can not have a value of 1, 2 or 3 at the same time. You could therefore have tested for those values in any order.

If you want to perform hierarchical tests and combined conditions, that order becomes more critical. The code below does not take into account the sequential nature of the test and fails to implement a hierarchical test:

Case of

: (vResult = 1)
... `Do this
: ((vResult = 1) & (vCondition2=2))
... `Do that

End case

When 4D executes that piece of code, it first checks the value of vResult. If that value is 1, the code placed below the first condition is executed and the second condition will NEVER be reached. Since the second condition tests for the same value of vResult and is more restrictive, it should be placed first in the test sequence.Therefore, the proper order is as follows:

Case of

: ((vResult = 1) & (vCondition2=2))
... `Do this
: (vResult = 1)
... `Do that

End case