Sunday, 20 June 2010

Record’s variant

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.

0 comments:

Post a Comment

These links are part of a pay per click advertising program called Infolinks. Infolinks is an In Text advertising service; they take my text and create links within it. If you hover your mouse over these double underlined links, you will see a small dialog box containing an advertisement related to the text. You can choose to move the mouse away and go on with your browsing, or to click on the box and visit the relevant ad. Click here to learn more about Infolinks Double Underline Link Ads.