With Keyword in Delphi
With Keyword in Delphi
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.
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;
Output
John
Smith
7 Park Drive
Branston
Grimworth
Banmore
BNM 1AB
Sarah
Smith
7 Park Drive
Branston
Grimworth
Banmore
BNM 1AB