KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: A conceptual framework to assist those beginning to code in 4D
PRODUCT: 4D | VERSION: | PLATFORM: Mac & Win
Published On: April 25, 2002

It has been said that in order to get a correct answer you must ask the correct question.
When starting out coding in 4D, the natural question to ask is, "Where do I put the code?"
Since 4D has Object Methods, Form Methods, Triggers, Project Methods and Database Methods, some confusion as to where to place code is a common experience.

Here is a simple approach to getting started with coding in 4D, that you might consider to be the Rosetta Stone for understanding where to place your code. Simply take the question, "Where should I put the code?" and reframe the question to be "When do I want this code to execute?" That's it, just restate the question as, "When do I want this code to execute?" Of course reframing the question is just a start, to point you in the right direction. So let's consider when the code executes for each place that you can put code.

If you want code to execute when a user interacts with an object on a form, you can easily place the code in the Object Method (a method attached to the object). For example, code that you want to execute when a button is clicked or when a user types in a keyboard enterable area can be put in the object's method.

If you want code to execute when something is happening with the form, place the code in the Form Method (a method attached to a form), for example, if you want code to execute when the form is loading to be displayed.

If you want the code to be executed in response to the records of a table being loaded, saved or deleted, no matter what form a user is interacting with, or even if changes are being made to records by the execution of code without the use of a form (say, during an import), place the code in a Trigger (a method attached to a table). For example, if there is something that must be done any time a record is saved, such as time and date stamping, a trigger is the place to put the code. (Be careful, triggers must complete executions quickly.)

If you want code to be executed when a menu item is selected, or to be executed by a variety of Object Methods, Form Methods, Triggers, Project Methods, or Database Methods, then you will place the code in a Project Method (a method given its own name that can be called from any of the places mentioned).

If you want the code to be executed when an event occurs at the level of the database as a whole, then place the code in one of the Database Methods; for example, if you want the code to be executed on starting the database up.

Now, the descriptions I've used such as "when a user interacts with an object" or "when something is happening with the form" or "when data in a table is being saved" are general descriptions that I have used to communicate "the big picture." Actually these "user interactions" or "things happening with a form" or "events occurring at the level of the database as a whole" are further refined into distinct events such as the "On clicked" event (occurs when a user clicks on an object), or the "On load" event (occurs when the form is being loaded for display), or the "On saving a new record event", or the "On startup event". So you must familiarize yourself with the various form events for objects and forms, and the database events for Triggers and Database Methods, because it is these events that cause methods to execute, providing that the property allowing that event is turned on. However, events will not be discussed further here, other than to draw your attention to the need to learn about them.

In the beginning, you will probably find that you start out using Object Methods, then Form Methods, and then Project Methods; later graduating to using Triggers and Database Methods as your knowledge of 4D and coding develop. As you go along coding, there are added concepts that you will begin to explore as you gain more experience; topics such as the fact that:
· there is some overlap between Object Methods and From Methods, giving you the option of using either in some instances.
Or

· Project Methods are used extensively to segment code for reuse, maintainability and readability

But, the purpose of this tech tip is to provide a conceptual framework to assist you in beginning to code in 4D. Remember, "Where you put the code is determined by when you want it to execute!"