4D v13

Overview of variables

Home

 
4D v13
Overview of variables

Overview of variables    


 

 

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 life cycle of a variable is as follows:

StageSimple variableArrayComments
Birth = InitializationC_LONGINT(NumDays)ARRAY TEXT(ArrayDates;0)In Unicode mode, Alpha and Text are identical
INSERT ELEMENT(ArrayDates;1)
ArrayDates{1}:=!06/05/2012!
Growth = Value assignmentNumDays:=25OR
APPEND TO ARRAY (ArrayDates;!06/05/2012!)
"Development" public service =For($i;1;NumDays)$StartDate:=ArrayDates{1}+18Variables are in read/write
UseEnd for
Death = Erased from memory and freed memory spaceCLEAR VARIABLE (NumDays)CLEAR VARIABLE(ArrayDates)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:

  • 2D arrays
  • BLOBs
  • Pointers
  • and so on.

 
 

 
PROPERTIES 

Product: 4D
Theme: Overview of variables