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.

Friday 11 June 2010

Accessing Record’s Elements

After we declare a record ( previous tutorial ), now we are going to access the elements or fields of the record. The fields in a record can be accessed using this notation :

    RecordName.RecordField

Between the field name and the record name separated by a dot.

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
   ItemStock : TRecordItem;

begin
  // assigning values to record's fields
  ItemStock.Item := 'USB Flash Disk';
  ItemStock.Quantity := 20;
  ItemStock.Price := 10;
  // writing teh fields' values.
  writeln('Item : ',ItemStock.Item);
  writeln('Quantity : ',ItemStock.Quantity,' pc/s');
  writeln('Price : $ ',ItemStock.Price:10:2 );
  readln;
end.

Using with statement

With statement allows us to access a record field without mentioning the record name. The syntax of with statement is :

 with Record do
   begin
     statements
   end;

Example :

program Project1;
{$APPTYPE CONSOLE}
uses
   SysUtils;

type
   TRecordItem = record
     Item : string;
     Quantity : integer;
     Price : currency;
   end;
var
ItemStock : TRecordItem;

begin
  with ItemStock do
    begin
      // assigning values to record's fields
      Item := 'USB Flash Disk';
      Quantity := 20;
      Price := 10;
      // writing teh fields' values.
      writeln('Item : ',Item);
      writeln('Quantity : ',Quantity,' pc/s');
      writeln('Price : $ ',Price:10:2 );
    end;
  readln;
end.

You can download the complete code here.

Wednesday 9 June 2010

Record

Record is a structured data type. It contains some elements or fields, and each field can have a different data type. See the picture below.

How to declare a record ?

You have to declare a record after type clause.


type

    RecordTypeName = record

       Field_1 : DataType;

       .

       .

       Field_n : DataType;

    End;

var

   RecordName : RecordTypeName;

For example, we declare a record named ItemStock that has three fields ( Item, Quantity and Price ).


type

   TRecordItem = record

     Item : string;

     Quantity : integer;

     Price : currency;

   end;

var

   ItemStock : TRecordItem;


First, define a TRecordItem then use it to declare a variable named ItemStock. So the ItemStock variable has three fields : Item, Quantity and Price.

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.