User-defined types

Defining a new type

The definition of a user-defined type consists of a list of names together with their type. It describes the fields of a record. The form is:

 TYPE type_name

   type_of_field1 name_of_field1

   type_of_field2 name_of_field2

   . . .

 END TYPE type_name

 

For example:

TYPE ADDRESS
  CHARACTER(LEN=40)::Name
  INTEGER ::Number
  CHARACTER(LEN=40)::Street
  CHARACTER(LEN=40)::Town
  CHARACTER(LEN=10)::PostCode
END TYPE ADDRESS

Unlike an array, the components of a user-defined type need not have the same type.

A field type can be a user-defined type.

A field can be an array.

Declaring a type does not allocate storage for a variable of that type.

 

Declaring variables

Once a type has been defined, a declaration takes the form:

TYPE(type_name) list_of_variables

For example:

TYPE(ADDRESS) MyAddress,AddressBook(200)

AddressBook is an array with index from 1 to 200.

A double colon (::) is required if type attributes are included.

 

Referencing a field

In contrast to an array where a component is identified by its index, a component of a record is identified by its name. The percent sign (%) is used in this context. For example:

MyAddress%Number

AddressBook(10)%Street

 

Constructor

The fields of record can be given a value by simple assignment:

MyAddress%Number=12

or by using a constructor that takes the form of a function whose name is that of the type and whose arguments are the initial values for the fields.

MyAddress=ADDRESS("John Smith",12,"Main St","Salford","M6 4NZ")

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited