Thursday 18 July 2013

JULY 18


Learned about how to create our own data types as well as its examples to implement it.

Defining Types

WE can define our own data types using the types statement and base them on existing data types.

Syntax for the TYPES Statement

The following is the syntax for defining a type using the types statement.
types t1[(l)] [type t] [decimals d].
or
types t1 like v1.
where:
  • t1 is the type name.
  • v1 is the name of a variable previously defined in the program, or is the name of a field that belongs to a table or structure in the Data Dictionary.
  • (l) is the internal length specification.
  • t is the data type.
  • d is the number of decimal places (used only with type p).
Listing below examples of programs that define and use their own data types:
Example-1:


Simple Example of a User-Defined Data Type "CHAR2"

1 report ztx0811.
2 types char2(2) type c.
3 data: v1 type char2 value 'AB',
4       v2 type char2 value 'CD'.
5
6 write: v1, v2.
Output:

AB CD

Description:
Line 2 defines a data type named char2. It is a two-byte char field. On lines 3 and 4, variables v1 and v2 are defined as two-byte char fields using the data type char2. They are given default values and, on line 6, they are written out.

Example-2:

Using Types Can Make the Code Clearer and Easier to Read
1  report ztx0812.
2  types: dollars(16) type p decimals 2,
3         lira(16)    type p decimals 0.     "italian lira have no 
        decimals
4
5  data: begin of american_sums,
6             petty_cash type dollars,
7             pay_outs   type dollars,
8             lump_sums  type dollars,
9             end of american_sums,
10        begin of italian_sums,
11            petty_cash  type lira,
12            pay_outs    type lira,
13            lump_sums   type lira,
14            end of italian_sums.
15
16 american_sums-pay_outs = '9500.03'.       "need quotes when literal 
     contains a decimal
17 italian_sums-lump_sums = 5141.
18
19 write: / american_sums-pay_outs,
20        / italian_sums-lump_sums.
Output:
9,500.00
5,141
Description:
Line 2 defines a data type named dollars as a 16-byte packed decimal field with two decimal places. Line 3 defines a similar data type named lira with 0 decimal places. On lines 5 through 14, two field strings are defined using the new data types. One component of each is assigned a value on lines 16 and 17, and on lines 19 and 20 they are written out.


  •  The same rules apply to types as apply to variables and field strings. Type names, like variable names, are one to 30 characters long, but unlike variables, their names cannot include the characters - < >.