Code is a general term used for instructions for a computer to execute written in a computer language. A method is one set of such instructions. Other terms you may encounter that are frequently used in place of "code" or "method" are "procedure" or "program". Those of you who are new to programming, who have worked with programs such as Excel or FileMaker, maybe familiar with the term "script". A script and a method are essentially the same; they are a computer program or code.
4th Dimension has 5 places called methods where you can place code:
The Rosetta Stone for coding in 4th Dimension is as follows:
This statement implies that there are specific times when each of kind of method will execute, and that is absolutely true.
Here are the times when each kind of method executes:
Now, the preceding statements have used the rather general description "when something happens". Actually, the times when each kind of method executes are quite precise, for their execution is triggered by occurrence of specific events.
In a graphical user environment, there is a high degree of interaction between users and their computers through objects and forms that are presented on screen. In addition, what is displayed on screen, and where one is in the application (startup, viewing a form, interacting with objects on a form, saving data, shut down, etc.) changes constantly, so it must be possible to cause code to execute based upon what is happening in the application. 4th Dimension uses events that are specific to objects, forms, tables and the database as a whole to cause code to execute at specific times, when specific activities are taking place.
Suppose a form is about to be displayed, and you want your code to execute before the form is displayed on screen. For example, perhaps in a Contacts database, the contact's name is properly stored in two fields, First Name and Last Name, but you wish to display the first name and last name on a form as a single string. The code to do this would have to execute while the form was loading, so that when the form was displayed the name would be appear in the desired manner. Getting this code to run at the proper time is simple because 4th Dimension generates an On Load event for the form, which causes code to run when the form is loading.
Or perhaps you want to capitalize the contents of a field when the user tabs out of the field. Well, there is an event that will occur when the user tabs out of a field if the data in a field has been changed. That event is the On data change event. The occurrence of this event will cause code placed in the object method of the field to execute. Therefore we would place the code to capitalize the contents of the field in the object method of that field.
Now, where you put the code is determined by when you want it to execute. Another way of saying this is to say that where you put the code is determined by what event you want the code to execute for. If you want the code to execute in response to an On load event for the form, then you would put the code in the form method. If you want the code to execute in response to an On data change event for an object (for example a field) then you would put the code in the object method of that object. It should be understood that this is a general guideline, and that in fact it is often the case that the code could be put in either a form method or an object method, but this general guideline will get you started understanding where to place code, and it will never be wrong.
Given the constantly changing presentation of objects on the screen, and the variety of tasks that 4th Dimension must perform (presenting interface, storing data etc), and the high degree of user interaction provided by graphical operating systems; there are a significant number of events that 4th Dimension may generate. Events fall into two categories: Form events which have to do with forms and the objects on forms, and database events which have to do with tables and the operation of the database as a whole. These events are covered in detail in reference materials listed below.
To simplify things, it is suggested that you focus on using object methods when you begin coding in 4th Dimension. Always keep in mind that where you put code is determined by when you want it to execute. This simple guideline will assist you in using object methods, form methods, triggers and database methods. However there is one kind of method that we have not covered yet; Project methods.
Project methods are the only kind of method that is not associated with an object, a form, a table, or the database as whole. Therefore, unlike all other methods, project methods do not execute in response to an event. Rather, they are assigned a name by the programmer, and they are executed when they are called by name.
How can project methods be called? Project methods can be called by name from a menu item, an object method, a form method, a trigger, a database method, or another project method
Project methods can be called from a menu item, in fact, a menu item must call a project method; this is how menu items are made to work.
Or, code that might be copied and pasted into multiple object methods, form methods, or triggers for re-use, should instead be placed in a single project method. Then that project method's name can be placed in the object methods, form methods, or triggers, to be called when those methods are run. This technique prevents duplicate copies of code from being placed in multiple methods in an application. One should avoid duplicating code, because any changes made to one instance of that code must be made to all instances in order to maintain consistency, and each instance of that code must be tested in order to detect any errors. Thus, duplicate code makes revising your code a much more complex and time consuming task, and should absolutely be avoided.
Additionally, code that might be placed in a single long project method, is frequently divided up by function and placed in separate project methods to be called by name as subroutines. This technique makes each method a discreet logical unit, making the code easier to understand, making re-use and revision of the code easier.
Frequently, in order to re-use code, it is necessary to pass values, known as parameters, to a Project Method. Information on this technique can be found in the listed learning resources below, under the topic "Passing Parameters".
Finally, project methods are loaded and unload from memory individually, so a series of short project methods will make more efficient use of memory than one long project method, since methods stay in memory in their entirety until they finish execution.
Remember the Rosetta stone for understanding the different types of methods (other than project methods) and events and available in 4th Dimension: Where you put code is determined by when you want it to execute; that is what event you want the code to execute for.
When learning to work with object methods, form methods, triggers, database methods, and events, do the following:
And, remember that Project methods are used:
Finally, remember that to simplify things, it is suggested that you focus on using object methods when you begin coding in 4th Dimension.
Implementing A Combo Box
This video tech tip explains the code, methods and events used for building a combo box.
4th Dimension Design Reference (PDF)
Chapter 5, Pages 421 – 427: Describes using object methods and events with fields and objects.
Chapter 7 Pages 477 – 528: Covers the following topics: 4th Dimensions 5 kinds of methods, events, creating and editing methods, the list method editor, the flowchart method editor*
*It is suggested that you avoid using the flowchart editor. The best way to lean 4th Dimensions language is to enter your code in the list editor. The two editors are mutually exclusive; methods created in one of the editors can not be opened in the other.
Methods
This web page introduces Object methods, Form methods, Table methods (Triggers), Project methods, and Database methods
Project methods
This web page covers the flowing uses of Project methods: menu methods, subroutines, Passing Parameters to Methods, Functions
Operators
This web page covers the topics of Precedence, and the Assignment Operator, and provides links to pages which cover the specific use of String Operators, Numeric Operators, Date Operators, Time Operators, Comparison Operators, Logical Operators, Picture Operators, Bitwise Operators
Control Flow
This web page covers the topics of the control and flow of execution in 4th Dimension methods. Sequential structures and Branching structures are covered and links are provided to pages covering 4D's Looping structures
Form event function documentation
Form events and the use of the form event function are explained.
Of special interest for FileMaker developers converting FileMaker databases to 4D databases:
Emulating the FileMaker Pro Book Object in 4D
Emulating the FileMaker Pro Book Object in 4D, Part II
These two web pages show how to recreate the FileMaker book record navigation tool in 4D. They are interesting examples of using the 4th Dimension language, in the context of creating a user tool to aid users transitions to working with 4D.
4D v 6.8 Getting Started (book)
Chapter 12, Pages 211 – 228: These pages cover the different kinds of methods, Control statements, Operators, and some syntax.