13.2.8 The With statement

The with statement serves to access the elements of a record or object or class, without having to specify the element’s name each time. The syntax for a with statement is

With statement

--            -|-             ---  -         -------------------
  with statement  |-variable-reference-| do  statement

The variable reference must be a variable of a record, object or class type. In the with statement, any variable reference, or method reference is checked to see if it is a field or method of the record or object or class. If so, then that field is accessed, or that method is called. Given the declaration:

  Passenger = Record  
    Name : String[30];  
    Flight : String[10];  
  TheCustomer : Passenger;

The following statements are completely equivalent:

TheCustomer.Name := ’Michael’;  
TheCustomer.Flight := ’PS901’;


With TheCustomer do  
  Name := ’Michael’;  
  Flight := ’PS901’;  

The statement

With A,B,C,D do Statement;

is equivalent to

With A do  
 With B do  
  With C do  
   With D do Statement;

This also is a clear example of the fact that the variables are tried last to first, i.e., when the compiler encounters a variable reference, it will first check if it is a field or method of the last variable. If not, then it will check the last-but-one, and so on. The following example shows this;

Program testw;  
Type AR = record  
      X,Y : Longint;  
     PAR = ^Ar;  
Var S,T : Ar;  
  S.X := 1;S.Y := 1;  
  T.X := 2;T.Y := 2;  
  With S,T do  
    WriteLn (X,’ ’,Y);  

The output of this program is

2 2

Showing thus that the X,Y in the WriteLn statement match the T record variable.

Remark: When using a With statement with a pointer, or a class, it is not permitted to change the pointer or the class in the With block. With the definitions of the previous example, the following illustrates what it is about:

Var p : PAR;  
  With P^ do  
   // Do some operations  
   X:=0.0;  // Wrong X will be used !!  

The reason the pointer cannot be changed is that the address is stored by the compiler in a temporary register. Changing the pointer won’t change the temporary address. The same is true for classes.