User-Defined Types (UDTs)

Arrays are useful when you need to treat a set of similar variables as a unit. For instance, ten test scores or ten student names. But what if you need to store several unrelated data types and be able to treat them as a unit? That is where User-Defined Types come in. When you define a User-Defined Type (UDT), you are actually defining a template for a new variable Type.

Once created, you can define as many variables of your new Type as you please. Moreover, since User-Defined Types can be associated with a random file's buffer, this provides you with a whole new way to access your random files.

PowerBASIC's User-Defined Type is similar to a C struct or Pascal record. The elements of a User-Defined Type may include any of PowerBASIC's data types, with the exception of dynamic (variable-length) strings, field strings, and arrays of dynamic strings.

To get an idea of the power of the User-Defined Type, imagine you are a teacher who needs a program to keep track of student grades. Since your school is on a very tight budget (and what schools aren't these days?), you decide to write the program yourself in PowerBASIC. For each student in the class you need to track the following information:

· | The student's name

· | A student number

· | A mailing address

· | The name and phone of the person to contact in case of an emergency

· | The relationship of the contact person to the student

Currently these records are being kept in a small file box. The information about each student is contained on a single file card. How do you transfer this information to the computer? Simple. Define a Student Record Type that will contain all the information about a single student.

The variables you create as User-Defined Types are often called records or record variables, since each variable of that Type contains one record, or one set of related information. The individual elements are referred to as fields or members. In the example above, each set of student information is a record, and each piece of information within that record (the last name for example) is a field.

See Also

Accessing the fields of a User-Defined Type

Nesting User-Defined Types

Arrays within User-Defined Types

Using arrays of User-Defined Types

Using User-Defined Types with procedures and functions

Storage requirements and restrictions

Unions