When the solution matters

Tips...

Registration Documentation Knowledgebase Seminars / Training Technical Support Partner Central

Step 10) Variables

A variable is an area in memory (RAM) reserved under a name, and referred to by that name, into which a value can be assigned. This value can be changed and retrieved by referencing the variable by name. Since the values in variables are stored in RAM, they are volatile; they do not persist once you quit the application.

Prerequisites

We suggest that you review the contents of the following pages and the learning resources listed on those pages

Why are variables used?

Computers have two types of storage; permanent and temporary.

Data that is stored permanently is written to disk. In a database, the data in fields is stored as part records, and the records are written to disk.

However there are times when it is necessary to temporarily manipulate data, or store a value to monitor an operation. In these instances, the values do not need to be written to disk to be saved permanently. In such cases, rather than storing the value in a field, a variable is used.

For example, in a Contacts database, a contact's name would properly be stored in two fields, First Name and Last Name. However, when displaying or printing a contact's information, it might be desirable to display the first name and last name as a single string. This can be accomplished by assigning the first and last name into a single variable, and then placing a variable object, with the same variable name, on a form to display the resulting string. There would be no need to store this string in a field, since the data already exists in the First Name and Last Name fields.

Another example variable usage would be using a variable to monitor whether a user clicked on a particular button. Suppose you had a series of radio buttons, and you wanted to know which button the user clicked on. If there was a variable associated with the button, and that variable was set to a value of 1 when a user clicked on it, you would have a way to monitor which button the user clicked on.

How are variables created?

Variables are created (or declared) in 4th Dimension in two ways:

  1. When writing code, variables can be created by typing a variable name into the method editor and assigning a value to that variable. In this case, 4th Dimension automatically creates and maintains a variable simply by using it in the method editor. No formal definition of variables is required; it's handled for you automatically by 4th Dimension.
  2. By placing an active object on a form: Objects created with the active object tools of the form editor, automatically have a variable created that is associated with that object. That variable will automatically have the same name as the active object, and that variable will hold a value that reflects the user's interaction with the object.

Variables and data types

There are equivalent variable types for each of the data types that 4th Dimension stores in fields: String, Text, Date, etc.

4th Dimension will automatically type variables based on the data assigned to them. However it is best that you type variables yourself using the compiler directives of 4th Dimension's language, this way there will be no ambiguity in the variable's type if you later decide to compile your application. A compiler directive simply tells 4th Dimension what type to use when it creates variables that you want to use. There are several commands in the language used to declare variables in this manner:

C_TEXT, C_REAL, C_DATE etc.

To declare a variable called myText to store text information, you'd write:

C_TEXT (myText)

Once you have typed a variable using 4th Dimension's compiler directives, it can only be assigned the type of data specified by the compiler directive. Again, while it's not essential that you do this because 4th Dimension will make its best guess on variable's type based on how you use it, using the compiler directives will eliminate any ambiguity when assigning variable types.

How are values assigned to variables?

  1. When writing code, values are assigned to variables by typing the variable name on the left, followed by the assignment operator (the combination of a colon and an equals sign), which is followed by the value. In the example below, a variable named Welcome gets assigned the value of the string "Hello":

    Welcome:="Hello"
  2. When a user interacts with an active object on a form, a value is automatically placed in the variable associated with that active object. For example if a user clicks on a button, the value of "1" is automatically assigned the variable that has the same name as the button.

All variables contain a single value, except for a class of variables called arrays, which can hold multiple values. Array's will be covered fully in the section of this tutorial titled "Arrays".

How are values retrieved from variables?

Values are retrieved from variables by typing the name of a field or variable on the left, followed by the assignment operator (the combination of a colon and an equals sign), followed by the variable that holds the value you wish to retrieve. In the example below, a field named LastName gets assigned the value in the variable named "LastNameVariable"

[Contacts]LastName:=LastNameVariable

When you assign the value in a variable to a field or another variable, you are assigning a copy of the value; the value of the source variable is unchanged.
Of course the data type of the value in the source variable and the receiving field or variable must be the same.

Notice the use of the Table name [Contacts] preceding the field name LastName. The use of the table name distinguishes the filed form the variable. In 4th Dimension's language table names are enclosed in brackets.
What is meant by the concept of a variable's scope?

Variables have a quality known as Scope:

When do you use local, process and inter-process variables?

How do you decide whether to declare a local, process or inter-process variable?
When coding in 4th Dimension it is desirable to use local variables because they are automatically cleared from memory, freeing that memory for other uses, when the method in which they were declared finishes execution. So, use local variables when possible; that is, when the variable does not need to be read beyond the method in which it is declared.

Process variables should be used when a variable's value needs to be read in methods other than the method in which the variable was declared, but within the same process. Process variables persist; they are not automatically cleared from memory (until you quit 4th Dimension). Therefore they should be assigned a null value when the value that they contain is no longer needed.

Inter-process variables should be used when a variable's value needs to be read in methods that are running in processes other than the process of the method in which the variable was declared. Inter-process variables persist; they are not automatically cleared from memory (until you quit 4th Dimension). Therefore they should be assigned a null value when the value that they contain is no longer needed.

Learning Resources

The following links will provide you with information concerning variables and their use in 4th Dimension.

4th Dimension Design Reference
Page 323: Definition of an active object
Pages 378-379: Lists of the types of active objects in 4th Dimension
Pages 379-380: Describes the use of enterable and non-enterable display variables on forms
Page 383: A note concerning the values automatically assigned to buttons

Variables
Data Types, Creating Variables, Assigning Data to Variables, Local Variables, Process Variables, Interprocess Variables, Form Object Variables, System Variables

4D v 6.8 Getting Started
Pages 225-230
These pages cover the following topics concerning variables:
Definition, Assignment and Equality, Creating a variable, Types of Objects, The Scope of variables


International | Company | Contact 4D | Site Map | Privacy Policy | © 4D UK 1995-2008 | Change font size: [A] [A] [A] | Print this page