In this section, we're going to learn how to set the types of variables, understand their scope and life cycle and cover the basics of their programming.
In previous videos, we have used variables on occasion (such as vNumRecords for instance). Now let's take a closer look at exactly what they are, how they work and, depending on their type, what their scope of use is. If we compare how 4D works to how a company works, we can infer the following:
There are several departments in the company that each perform a particular task, often independently from other departments.
Each department is designed to perform a certain number of tasks in a specific order.
A task can be interrupted because it depends on the outcome of another process.
This new process probably uses information coming from a prior process in addition to its own specific information.
If we want to convey this using a practical example:
The company has a production facility, a sales department and an HR department.
The Payroll department centralizes the hours worked by the other departments and pays their salaries, calculates the number of vacation days due, and so on.
Paying salaries means knowing the overtime rate as well as the different pay and tax rates. This information is provided by the legal department which keeps up-to-date documentation.
And now let's make the comparison with 4D:
4D can manage many processes simultaneously (printing, viewing the contents of several tables, tool palettes, imports, Web server, responding to Web services, and so on)
The method that executes in each process can include several different phases.
It can call other methods within the same process (coworkers from the same department), or request information from another process (coworkers from a different department)
For each case, we have suitable variables available:
To have information available (in read/write mode) for all processes, we use interprocess variables. For 4D to consider a variable as interprocess, it must be prefixed with the <> symbols (for example: <>CurrentDate, <>Rate-TimeTable, and so on)
While a process is executing, a method may need information specifically for this process alone. In this case, this is a local variable, which 4D recognizes by its $ symbol prefix (for example: $Counter, $StampZone, and so on.)
All other variables (without prefixes) are process variables, used by several methods in the same process. (for example: vNumRecords, and so on)
The point we made concerning the need for information from the legal department is meant to illustrate the idea of inter-process communication.
WIth 4D, you can read or write variables from one process to another (and even from a client machine to the server) using GET PROCESS VARIABLE and SET PROCESS VARIABLE.
To use another example from everyday life, we can compare it to a school where:
The local variable is the student's notebook: only he can see it or read and write in it.
The process variable is the blackboard: it can be seen and used by all the students in the same class and each student can read it, write on it or erase it.
The interprocess variable is the bulletin board at the entrance to the school where exam results are posted: it is available to all the students in the school, as well as the teachers and the principal.
Interprocess communication is when one teacher comes to read or write on the class blackboard (process variable) of another teacher or on the school bulletin board (interprocess variable).
Now that we've covered the scope of variables, we can look at how they work.
There are two types of variables in 4D:
Simple variables (with a single value)
Arrays (with multiple values).
You can set simple variables using the same types as for fields (Text, Longint, Date, Time, BLOB, and so on) + the Pointer type. Arrays accept the same types except for BLOB and Time.
The variable still exists, its content is reinitialized
Let's look at this with an example:
To name your variables, get in the habit of following the same general procedure to keep things simple.
You can either use a "strict" nomenclature
Or you can opt for easy-to-read variable names that speak for themselves.
To begin with, it's always a good idea to use names that are clear and easy to read. You can always rename them later using the global search function in 4D.
As with any language, variables are indispensable in 4D. Feel free to make use of them whenever and wherever you can.
Keep in mind that certain variables cannot be seen in a form:
In the previous video, we used a variable to display the number of records found according to the table where the search was performed. In fact, a variable is a space in memory that we can represent by an object in a form.
So we're going to create a test project form called "test variables" in order to show how variables work.
Let's create a first variable named v1 and that we'll make non-enterable. This variable does not actually exist in memory; there is just an area on screen that represents the contents of the variable if and when it exists.
We're going to:
put a button next to it that we can use to declare the variable. We'll give this variable the Longint type
then a 2nd button that we can use to assign a value to this variable -- v1:=1500.
Next, we can perform a calculation with this variable in memory and display, for instance, an ALERT.
We're going to display an alert that expects an argument of the text type so we're going to convert variable v1 after multiplying it by 12.
Now let's test the form:
Variable v1 is not actually defined yet
If we declare it, then we declare the type as longint; it takes a default value of 0
We can assign a value to it
and then we can perform a calculation with this variable.