To access the contents, click the chapter and section titles.
Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97
Chapter 10 Using Variant Records
In this chapter, you are going to design a payroll system step by step. With each step, you learn more about variant records, an enhanced type of records, which give you more control over your program. By the end of this chapter, your payroll system will be completed.
10-1 Variant Records
In real life applications, the employees in the same company may fall into different categories. Some employees are salaried, some are paid on hourly basis, and others are paid by commission. The payroll for each of these categories uses different calculations. The following is an example of a record for a salaried employee:
SalariedEmployee = RECORD
ID :STRING[5];
Name :STRING[20];
Position :STRING[209];
SSN :STRING[11];
MonthlySalary :REAL
END;
An example of the record of a weekly-paid employee is:
HourlyEmployee = RECORD
ID :STRING[5];
Name :STRING[20];
Position :STRING[20];
SSN :STRING[11];
HourlyRate :REAL
END;
An example of the record of a salesperson paid by commission is:
CommissionEmployee = RECORD
ID :STRING[5];
Name :STRING[20];
Position :STRING[20]
SSN :STRING[11];
Commission :REAL;
BasicSalary :REAL;
Area :STRING[20]
END;
It is not a good idea to use three different records in the same program to represent the employee record. In Pascal the variant record allows the programmer to store different types of data in the same memory location. The variant record, in this example, will have a fixed part which contains the fields that do not change from one employee to the other (such as ID, Name, and SSN), and a variant part which differs from one category to another (such as the payroll details). In order to differentiate between different types of records, the variant record must be declared using a CASE structure with one of the fields as the case expression. This field is called the tag field.
An example of the tag field that you can add to the record is a character variable that may contain the values 1, 2, or 3 to represent the following categories:
- 1 = salaried employees
- 2 = hourly paid employees
- 3 = employees paid by commission
Here is the employee variant record:
SalariedEmployee = RECORD
ID :STRING[5];
Name, Position :STRING[20];
SSN :STRING[11];
CASE Category :CHAR OF
'1':(MonthlySalary :REAL);
'2':(HourlyRate :REAL);
'3':(Commission,
BasicSalary :REAL;
Area :STRING[20])
END;
The tag field here is Category. If the value of the tag field is 1, it will transfer the control to the salaried employee and the variable MonthlySalary becomes in effect. If it is 2, the control is transferred to the hourly paid employee and the variable HourlyRate is in effect. If it is 3, the three variables Commission, BasicSalary, and Area are all brought into action.
The variant record may contain a fixed part followed by a variant part, or may contain a variant part only. The declaration takes the following general form:
type-name = RECORD
fixed field-list
variant field-list
END;
The variant field list takes the following form:
CASE tag-field : type-definition OF
label-1 : (field-list : type-definition);
label-2 : (field-list : type-definition);
...
label-n : (field-list : type-definition);
Notice that the field list for each case is enclosed in parentheses, and the CASE structure does not contain an END statement.
|