4D v16

Overview of variables

Home

 
4D v16
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.

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.

Here is the first use of a simple variable.

The principle is the same with:

  • a variable of the text type that we'll call v2
  • Since it is a text type variable, we can display its value 12 times.
  • In passing, we concatenate the variable with a space and the combination will be multiplied by 12.
  • "Hello" here will be a variable not of the longint type, but of the text type.

If we test the form:

  • Variable v2 does not exist
  • When it is declared, it contains a blank value.
  • We can assign the value "Hello" to it
  • and use the variable in a calculation.

4D includes many functions for processing strings.

Let's take the case of an array.

  • We'll display it in a pop-up menu that we call pop1
  • and we declare that it will be of the array type
  • so to make a "Pop1" text array, we indicate the number of rows in the array, for instance 3 rows.
  • Now the array exists in memory and can be represented on this object.

To assign values to the array, we're going to indicate that:

  • the 1st row is "hello"
  • the 2nd row is "bye bye"
  • and the 3rd is "see you soon".

As concerns the use of the array, we can do it as shown here for example:

1st array value + a space + 2nd array value + 3rd array value.

Now let's test the result:

  • declaration of array: the array has 3 empty rows
  • assignment of values to array: "hello"  "bye bye"  "see you soon"
  • using the array: "hello space bye bye space see you soon"

So there we have it, a simple and quick overview of how to use variables.

 
 

 
PROPERTIES 

Product: 4D
Theme: Overview of variables

 
HISTORY 

 
ARTICLE USAGE

Self-training ( 4D v16)