DelphiBasics
with
Keyword
A means of simplifying references to structured variables
1 keyword with(with data name do Statement;
2 with data name do
 begin
  Statement;
  {...}
 end;
Description
The With keyword is a convenience provided by Delphi for referencing elements of a complex variable, such as a record or object.
 
It simplifies the code by removing the need to prefix each referenced element with the complex variable name.
 
For example :
 
myObject.colour := clRed;
myObject.size   := 23.5;
myObject.name   := 'Fred';

 
can be rewritten :
 
With myObject do
begin
  colour := clRed;
  size   := 23.5;
  name   := 'Fred';
end;

 
However be warned that it can surprisingly make your code more difficult to read, especially when nesting with clauses. More disturbingly, it can create maintenance problems, where a code change can mean that the wrong target for the 'child' field referenced.
Notes
Within a With clause, Delphi tries to resolve these 'incomplete' references by looking at the immediate with target, and then any nested withs above.

It then looks at other data types, such as local variables, subroutine parameters etc. This is where some of the danger of the with keyword comes.

Use carefully!
Related commands
BeginKeyword that starts a statement block
EndKeyword that terminates statement blocks
RecordA structured data type - holding fields of data
 Download this web site as a Windows program.




 
Example code : Using the with keyword with a record structure
type
  // Declare a customer record
  TCustomer = Record
    firstName : string[20];
    lastName  : string[20];
    address1  : string[100];
    address2  : string[100];
    address3  : string[100];
    city      : string[20];
    postCode  : string[8];
  end;

var
  John, Sarah : TCustomer;

begin
  // Set up the John's customer details
  With John do
  begin
    firstName := 'John';
    lastName  := 'Smith';
    address1  := '7 Park Drive';
    address2  := 'Branston';
    address3  := 'Grimworth';
    city      := 'Banmore';
    postCode  := 'BNM 1AB';
  end;

  // Set up John's sister similarly - simply copying the whole record
  Sarah := John;

  // And then changing the first name to suit
  Sarah.firstName := 'Sarah';

  // Now show the details of both customers
  With John do ShowCustomer([firstName, lastName,
                             address1, address2, address3, city,
                             postCode]);
  With Sarah do ShowCustomer([firstName, lastName,
                             address1, address2, address3, city,
                             postCode]);
end;

// A procedure that displays a variable number of strings
procedure TForm1.ShowCustomer(const fields: array of string);
var
  i : Integer;

begin
  // Display all fields passed - note : arrays start at 0
  for i := 0 to Length(fields)-1 do
    ShowMessage(fields[i]);
end;
Show full unit code
  John
  Smith
  7 Park Drive
  Branston
  Grimworth
  Banmore
  BNM 1AB
  
  Sarah
  Smith
  7 Park Drive
  Branston
  Grimworth
  Banmore
  BNM 1AB
  
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page