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 |
Begin |
|
Keyword that starts a statement block |
End |
|
Keyword that terminates statement blocks |
Record |
|
A structured data type - holding fields of data |
|
|
|
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
|
|