4D v13.4

Introduction to the 4D Language

Home

 
4D v13.4
Introduction to the 4D Language

Introduction to the 4D Language  


 

 

The 4D language is made up of various components that help you perform tasks and manage your data.

  • Data types: Classifications of data in a database. See discussion in this section as well as the detailed discussion in the section Data Types.
  • Variables: Temporary storage places for data in memory. See detailed discussion in the section Variables.
  • Operators: Symbols that perform a calculation between two values. See discussion in this section as well as the detailed discussion in the section Operators and its subsections.
  • Expressions: Combinations of other components that result in a value. See discussion in this section.
  • Commands: Built-in instructions to perform an action. All 4D commands, such as ADD RECORD, are described in this manual, grouped by theme; when necessary, the theme is preceded by an introductory section. You can use 4D Plug-ins to add new commands to your 4D development environment. For example, once you have added the 4D Write Plug-in to your 4D system, the 4D Write commands become available for creating and manipulating word-processing documents.
  • Methods: Instructions that you write using all parts of the language listed here. See discussion in the section Methods and its subsections.

This section introduces Data Types, Operators, and Expressions. For the other components, refer to the sections cited above.

In addition:

  • Language components, such as variables, have names called Identifiers. For a detailed discussion about identifiers and the rules for naming objects, refer to the section Identifiers.
  • To learn more about array variables, refer to the section Arrays.
  • To learn more about BLOB variables, refer to the section DISABLE MENU ITEM.
  • If you plan to compile your database, refer to the section Compiler Commands as well as the Design Reference manual of 4D.

In the language, the various types of data that can be stored in a 4D database are referred to as data types. There are seven basic data types: string, numeric, date, time, Boolean, picture, and pointer.

  • String: A series of characters, such as “Hello there”. Alpha and Text fields, and string and text variables, are of the string data type.
  • Numeric: Numbers, such as 2 or 1,000.67. Integer, Long Integer, and Real fields and variables are of the numeric data type.
  • Date: Calendar dates, such as 1/20/89. Date fields and variables are of the date data type.
  • Time: Times, including hours, minutes, and seconds, such as 1:00:00 or 4:35:30 PM. Time fields and variables are of the time data type.
  • Boolean: Logical values of TRUE or FALSE. Boolean fields and variables are of the Boolean data type.
  • Picture: Picture fields and variables are of the picture data type.
  • Pointer: A special type of data used in advanced programming. Pointer variables are of the pointer data type. There is no corresponding field type.

Note that in the list of data types, the string and numeric data types are associated with more than one type of field. When data is put into a field, the language automatically converts the data to the correct type for the field. For example, if an integer field is used, its data is automatically treated as numeric. In other words, you need not worry about mixing similar field types when using the language; it will manage them for you.

However, when using the language it is important that you do not mix different data types. In the same way that it makes no sense to store “ABC” in a Date field, it makes no sense to put “ABC” in a variable used for dates. In most cases, 4D is very tolerant and will try to make sense of what you are doing. For example, if you add a number to a date, 4D will assume that you want to add that number of days to the date, but if you try to add a string to a date, 4D will tell you that the operation cannot work.

There are cases in which you need to store data as one type and use it as another type. The language contains a full complement of commands that let you convert from one data type to another. For example, you may need to create a part number that starts with a number and ends with characters such as “abc”. In this case, you might write:

 [Products]Part Number:=String(Number)+"abc"

If Number is 17, then [Products]Part Number will get the string “17abc”.

The data types are fully defined in the section Data Types.

When you use the language, it is rare that you will simply want a piece of data. It is more likely that you will want to do something to or with that data. You perform such calculations with operators. Operators, in general, take two pieces of data and perform an operation on them that results in a new piece of data. You are already familiar with many operators. For example, 1 + 2 uses the addition (or plus sign) operator to add two numbers together, and the result is 3. This table shows some familiar numeric operators:

OperatorOperationExample
+Addition1 + 2 results in 3
Subtraction3 – 2 results in 1
*Multiplication2 * 3 results in 6
/Division6 / 2 results in 3

Numeric operators are just one type of operator available to you. 4D supports many different types of data, such as numbers, text, dates, and pictures, so there are operators that perform operations on these different data types.

The same symbols are often used for different operations, depending on the data type. For example, the plus sign (+) performs different operations with different data:

Data TypeOperationExample
NumberAddition1 + 2 adds the numbers and results in 3
StringConcatenation“Hello ” + “there” concatenates (joins together)
the strings and results in “Hello there”
Date and NumberDate addition!1/1/1989! + 20 adds 20 days to the date
January 1, 1989, and results in the date
January 21, 1989

The operators are fully defined in the chapter Operators and its subsections.

Simply put, expressions return a value. In fact, when using the 4D language, you use expressions all the time and tend to think of them only in terms of the value they represent. Expressions are also sometimes referred to as formulas.

Expressions are made up of almost all the other parts of the language: commands, operators, variables, and fields. You use expressions to build statements (lines of code), which in turn are used to build methods. The language uses expressions wherever it needs a piece of data.

Expressions rarely “stand alone.” There are only a few places in 4D where an expression can be used by itself:

  • Query by Formula dialog box
  • Debugger where the value of expressions can be checked
  • Apply Formula dialog box
  • Quick Report editor as a formula for a column

An expression can simply be a constant, such as the number 4 or the string “Hello.” As the name implies, a constant’s value never changes. It is when operators are introduced that expressions start to get interesting. In preceding sections you have already seen expressions that use operators. For example, 4 + 2 is an expression that uses the addition operator to add two numbers together and return the result 6.

You refer to an expression by the data type it returns. There are seven expression types:

  • String expression
  • Numeric expression (also referred to as number)
  • Date expression
  • Time expression
  • Boolean expression
  • Picture expression
  • Pointer expression

The following table gives examples of each of the seven types of expressions.

ExpressionTypeExplanation
“Hello”StringThe word Hello is a string constant,
indicated by the double quotation marks.
“Hello ” + “there”StringTwo strings, “Hello ” and “there”,
are added together (concatenated)
with the string concatenation operator (+).
The string “Hello there” is returned.
“Mr. ” + [People]NameStringTwo strings are concatenated:
the string “Mr. ” and the current value
of the Name field in the People table.
If the field contains “Smith”, the expression
returns “Mr. Smith”.
Uppercase (“smith”)StringThis expression uses “Uppercase”,
a command from the language,
to convert the string “smith” to uppercase.
It returns “SMITH”.
4Number This is a number constant, 4.
4 * 2NumberTwo numbers, 4 and 2, are multiplied
using the multiplication operator (*).
The result is the number 8.
My ButtonNumberThis is the name of a button.
It returns the current value of the button:
1 if it was clicked, 0 if not.
!1/25/97!DateThis is a date constant for the date 1/25/97
(January 25, 1997).
Current date + 30DateThis is a date expression that uses
the command “Current date” to get today’s date.
It adds 30 days to today’s date and returns
the new date.
?8:05:30?TimeThis is a time constant that represents 8 hours,
5 minutes, and 30 seconds.
?2:03:04? + ?1:02:03?TimeThis expression adds two times together and
returns the time 3:05:07.
TrueBooleanThis command returns the Boolean value TRUE.
10 # 20BooleanThis is a logical comparison between two numbers.
The number sign (#) means “is not equal to”.
Since 10 “is not equal to” 20, the expression
returns TRUE.
“ABC” = “XYZ”BooleanThis is a logical comparison between two strings.
They are not equal, so the expression returns FALSE.
My Picture + 50PictureThis expression takes the picture in My Picture,
moves it 50 pixels to the right, and returns
the resulting picture.
->[People]NamePointerThis expression returns a pointer to the field
called [People]Name.
Table (1)PointerThis is a command that returns a pointer to
the first table.

 
PROPERTIES 

Product: 4D
Theme: Language definition

 
SEE ALSO 

Arrays
Constants
Data Types
Methods
Operators
Pointers
Variables