A record can have one or more variant elements or fields. This variant allows data to accessed using one or more different fields. To declare a record contain variant use these statements :
Type
RecordTypeName = record
Field_1 : type_1;
..
..
Field_n : type_n;
Case Tag : OrdinalType of
Constant_1 : ( variant_1);
Constant_2 : ( variant_2);
End;
Example record contain variant :
Type
TCategory = (FullTimer, PartTimer);
TEmployeeRecord : record
Name : String(30);
Case Category : TCategory of
Full Time : ( Salary : currency;
Bonus : currency);
Part Time : ( Salary : currency);
End;
Example program :
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TCategory = ( FullTimer, PartTimer );
TEmployeeRecord = record
Name : String[30];
Case Category : TCategory of
FullTimer : ( Salary : currency;
Bonus : currency);
PartTimer : ( Payment : currency);
end;
var
Employee : TEmployeeRecord;
begin
with Employee do
begin
// assigning fields' values
Name := 'Albert Einstein';
Category := FullTimer;
Salary := 1000;
Bonus := 100;
// writing fields' values
writeln('Name : ',Name);
writeln('Salary : ',Salary:10:2);
writeln('Bonus : ',Bonus:10:2);
// assigning fields' values
Name := 'Albert Einstein';
Category := PartTimer;
Payment := 500;
// writing fields' values
writeln('Name : ',Name);
writeln('Payment : ',Payment:10:2);
end;
readln;
end.
You can download the complete code here.