KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Always wrap Form and Object Method code based on Form Events
PRODUCT: 4D | VERSION: 11 | PLATFORM: Mac & Win
Published On: March 26, 2010

It is good practice to always wrap your form method and object method code with a Case statement and the Form event command.

For example, you could have an object on a form whose code is not wrapped in a form event case statement. Then any code written in that object method will be executed for every form event checked in that object's property list. Some developers do this on purpose, and then only check one event from the property list. This can be problematic if you later check other form events and add code for them; but your initial code will now run for all the events that were checked.

It is a good practice to avoid this potential problem. It is easily avoided by using a case statement such as the following:

C_LONGINT($LFormEvent)
$LFormEvent:=Form event

Case of
   :($LFormEvent=On Load )

     ` Do stuff while this object in its On Load cycle

   :($LFormEvent=On Unload )

     ` do stuff while this object is in its On Unload cycle

   :($LFormEvent=On Clicked)

     ` do stuff when this object is clicked

End case


The code above still depends on the corresponding form event being checked in the objects property list:



Following this simple rule of using the Form event command will make sure that the code you write is only executed during the specified form event.