home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Accessing Fields

Each field in a record can be accessed using both the record identifier and the field identifier separated by a period. For example, you can assign values to the fields with statements like:

    EmployeeRec.Name:= 'Charles A. Dixon';
    EmployeeRec.Rate:= 22.5;

You can do the same thing with input and output operations:

    WRITELN('Employee Name: ', EmployeeRec.Name);

This type of compound variable is called a fielded variable. Actually, the scope of the field identifier (such as “Name”) is the record in which it was declared, and it may be used elsewhere in the program as the name of another variable if desired.

In the following example, the record “EmployeeRec” is filled and then displayed.

{------------------------------- figure 8-2 -------------------------------}
PROGRAM RecordExample1(OUTPUT);
TYPE
  EmployeeRecord = RECORD
           Name                     :STRING[25];
           Address          :STRING[40];
           Phone                    :STRING[12];
           Rate                     :REAL;
           MaritalStatus    :CHAR;
          END;
VAR
  EmployeeRec:EmployeeRecord;
BEGIN
{ Assign values to the fielded variables }
  EmployeeRec.Name:= 'Diane J. Bedford';
  EmployeeRec.Address:= '20 Carmen Avenue, New Orleans, LA 70112';
  EmployeeRec.Phone:= '504-666-5043';
  EmployeeRec.Rate:= 28.5;
  EmployeeRec.MaritalStatus:= 'S';
{ Display record information }
  WRITELN('Employee Name: ', EmployeeRec.Name);
  WRITELN('Address: ', EmployeeRec.Address);
  WRITELN('Telephone #: ', EmployeeRec.Phone);
  WRITELN('Hourly Rate: $', EmployeeRec.Rate:0:2);
  WRITELN('Marital Status: ', EmployeeRec.MaritalStatus)
END.

The output is:

Employee Name:  Diane J. Bedford
Address:   20 Carmen Avenue, New Orleans, LA 70112
Telephone #: 504-666-5043
Hourly Rate: $28.50
Marital Status: S

The WITH Statement

There is, however, a shorter way to do this by using the WITH statement. When you use the WITH statement, you do not have to use fielded variables. Look at this block of assignments using the WITH statement:

    WITH EmployeeRec DO
     BEGIN
      Name:= 'Charles A. Dixon';
      Address:= '202 Greenwood, Gretna, LA 70088';
      Phone:= '504-666-7574';
      Rate:= 22.5;
      MaritalStatus:= 'M'
     END;

The effect of using the WITH statement is to attach each field name to the record name. If one of the variables inside the block is not a field identifier, it will not be modified by the WITH statement. If WITH is followed by only one statement, there is of course no need for the BEGIN-END block.

You can use the WITH statement to call a procedure to process the fields of a record, for example:

    WITH EmployeeRec DO
     DisplayResults(Name, Rate);

This statement is equivalent to:

    DisplayResults(EmployeeRec.Name, EmployeeRec.Rate);

The WITH statement takes the general form:

    WITH record-identifier DO
     statement;

The following example demonstrates the same logic as that used in program 8-2, but the program is divided into three subprograms: “GetData,” “DisplayInfo,” and “DrawLine” (which you wrote before). The output of this program is displayed in the proper format, using a header for the record.

{ ------------------------------- figure 8-3 ------------------------------- }
PROGRAM RecordExample2(OUTPUT);
TYPE
  EmployeeRecord = RECORD
            Name                 :STRING[25];
            Address         :STRING[40];
            Phone                :STRING[12];
            Rate                 :REAL;
            MaritalStatus   :CHAR;
                END;
VAR
  EmployeeRec :EmployeeRecord;
{ ------------ Procedure Drawline -------------- }
PROCEDURE DrawLine(LineLength, TabLength:INTEGER);
CONST
 Dash = '-';
VAR
 Counter:INTEGER;
BEGIN
 FOR Counter:= 1 TO TabLength DO
  WRITE(' ');
 FOR Counter:= 1 TO LineLength DO
  WRITE(Dash);
 WRITELN
END;
{ ------------- Procedure GetData -------------- }
PROCEDURE GetData(VAR Employee:EmployeeRecord);
{Name,Address,Phone,Rate,MaritalStatus);}
{ Assign values to fields }
BEGIN
 WITH Employee DO
  BEGIN
      Name:= 'Diane J. Bedford';
      Address:= '20 Carmen Avenue, New Orleans, LA 70112';
      Phone:= '504-666-5043';
      Rate:= 28.5;
      MaritalStatus:= 'S'
  END
END;
{ ----------- Procedure DisplayInfo ------------ }
PROCEDURE DisplayInfo(Employee:EmployeeRecord);
{ Display record information }
CONST
 Header ='Record of ';
VAR
 Len, Tab, Counter :INTEGER;
 HeaderText, Status:STRING;
BEGIN
 WITH Employee DO
  BEGIN
   HeaderText:= CONCAT(Header,Name);
   Len:= LENGTH(HeaderText);
   Tab:= (80- Len) DIV 2;
   DrawLine(Len, Tab);
   FOR Counter:= 1 TO Tab DO
     WRITE(' ');
   WRITELN(HeaderText);
   DrawLine(Len, Tab);
   WRITELN('Address:    ', Address);
   WRITELN('Telephone #:   ', Phone);
   WRITELN('Hourly Rate: $', Rate:0:2);
   IF MaritalStatus = 'M' THEN
     Status:= 'Married'
   ELSE
     Status:= 'Single';
   WRITELN('Marital Status: ', Status)
  END
END;
{ --------------- Main Program ----------------- }
BEGIN
 GetData(EmployeeRec);
 DisplayInfo(EmployeeRec)
END

The output is:

                 ---------------------
                 Record of Diane J. Bedford
                 ---------------------
Address:       20 Carmen Avenue, New Orleans, LA 70112
Telephone #:     504-666-5043
Hourly Rate:    $28.50
Marital Status: Single

The points which are worthy of your attention in the program are the use of the WITH statement and the passing of the record as a parameter to the subprograms. Notice also that the record is passed once as a variable parameter (using VAR), when it was to return values of the fields, and once as a value parameter, when it was only a receiver.

The actual value of such a program comes when it reads the employee information from a data file, which will be discussed shortly.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.