Back to Contents


User Types

Summary:

See also: Variables, Records, Data Types, Constants.


Definition

Purpose:

A user type is a data type based on built-in types, records or arrays.

Syntax:

TYPE identifier definition

Notes:

  1. identifier is the name of the user type to be defined.
  2. definition is any data type, record structure, or array definition supported by the language.

Usage:

You can define a user type as a synonym for an existing data type, or as a shortcut for records and array structures.

After declaring a user type, it can be used as a normal data type to define variables.

The scope of a user type is the same as for variables and constants. Types can be global, local to a module, or local to a function.


Examples

Example 1:

01 TYPE customer RECORD
02        cust_num INTEGER,
03        cust_name VARCHAR(50),
04        cust_addr VARCHAR(200)
05     END RECORD
06 DEFINE c customer

Example 2:

The following example defines the user type in a globals file and then uses the type in a report program:

01 -- typeglobals.4gl
02
03 GLOBALS
04  TYPE rpt_order RECORD
05        order_num  INTEGER,
06        store_num  INTEGER,
07        order_date DATE,
08        fac_code   CHAR(3)
09     END RECORD
10 END GLOBALS
01 -- report1.4gl
02
03 GLOBALS "typeglobals.4gl"
04
05 MAIN
06   DEFINE o rpt_order
07   CONNECT TO "custdemo"
08   DECLARE order_c CURSOR FOR
09     SELECT order_num, 
10            store_num, 
11            order_date, 
12            fac_code 
13       FROM orders
14   START REPORT order_list 
15   FOREACH order_c INTO o.*
16     OUTPUT TO REPORT order_list(o.*)
17   END FOREACH
18   FINISH REPORT order_list
19   DISCONNECT CURRENT
20 END MAIN 
21
22 REPORT order_list(ro)
23   DEFINE ro rpt_order
24
25   FORMAT
26     ON EVERY ROW
27      PRINT ro.order_num, 
...