Summary:
See also: Variables, Records, Data Types, Constants.
A user type is a data type based on built-in types, records or arrays. It can be used as a synonym for a single base type or structured types.
You typically define a user type for structured types (i.e. records), so it can be reused as a single word at many places in the code:
01
TYPE customer RECORD02
cust_num INTEGER,03
cust_name VARCHAR(50),04
cust_addr VARCHAR(200)05
END RECORD
A good practice is to define user types that belong to the same domain in a single .4gl module, and import that module in the modules where the types are needed.
[PUBLIC|PRIVATE] TYPE type-definition
[,...]
where type-definition is:
identifier { datatype | LIKE
[dbname:]tabname.colname }
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, module-specific, or local to a function.
By default, module-specific types are private; They cannot be used by an other module of the program. To make a module type public, add the PUBLIC keyword before TYPE. When a module type is declared as public, it can be referenced by another module by using the IMPORT instruction.
01
TYPE customer RECORD02
cust_num INTEGER,03
cust_name VARCHAR(50),04
cust_addr VARCHAR(200)05
END RECORD06
DEFINE c customer
The following example defines a user type in first module, and then uses the type in a report program:
01
-- type_order.4gl02
04
PUBLIC TYPE rpt_order RECORD05
order_num INTEGER,06
store_num INTEGER,07
order_date DATE,08
cust_num INTEGER,09
fac_code CHAR(3)10
END RECORD
01
-- report.4gl02
03
IMPORT FGL type_order04
05
MAIN06
DEFINE o rpt_order -- could be type_order.rpt_order07
CONNECT TO "custdemo"08
DECLARE order_c CURSOR FOR09
SELECT orders.*10
FROM orders ORDER BY cust_num11
START REPORT order_list12
FOREACH order_c INTO o.*13
OUTPUT TO REPORT order_list(o.*)14
END FOREACH15
FINISH REPORT order_list16
DISCONNECT CURRENT17
END MAIN18
19
REPORT order_list(ro)20
DEFINE ro rpt_order21
FORMAT22
ON EVERY ROW23
PRINT ro.order_num, ......