Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə54/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   50   51   52   53   54   55   56   57   ...   294
DelphiLanguageGuide

With Statements
A with statement is a shorthand for referencing the fields of a record or the fields, properties, and methods of an
object. The syntax of a with statement is
withobjdostatement
or
34


withobj1, ..., objndostatement
where obj is an expression yielding a reference to a record, object instance, class instance, interface or class type
(metaclass) instance, and statement is any simple or structured statement. Within the statement, you can refer to
fields, properties, and methods of obj using their identifiers alone, that is, without qualifiers.
For example, given the declarations
type 
  TDate = record
    Day: Integer;
    Month: Integer;
    Year: Integer;
  end;
var
  OrderDate: TDate;
you could write the following with statement.
with OrderDate do
 if Month = 12 then
   begin
     Month := 1;
     Year := Year + 1;
   end
 else
   Month := Month + 1;
you could write the following with statement.
if OrderDate.Month = 12 then
  begin
   OrderDate.Month := 1;
   OrderDate.Year := OrderDate.Year + 1;
  end
else
  OrderDate.Month := OrderDate.Month + 1;
If the interpretation of obj involves indexing arrays or dereferencing pointers, these actions are performed once,
before statement is executed. This makes with statements efficient as well as concise. It also means that
assignments to a variable within statement cannot affect the interpretation of obj during the current execution of the
with statement.
Each variable reference or method name in a with statement is interpreted, if possible, as a member of the specified
object or record. If there is another variable or method of the same name that you want to access from the with
statement, you need to prepend it with a qualifier, as in the following example.
with OrderDate do 
  begin
    Year := Unit1.Year;
       ...
  end;
35


When multiple objects or records appear after with, the entire statement is treated like a series of nested with
statements. Thus
withobj1, obj2, ..., objndostatement
is equivalent to
with obj1 do
  with obj2 do
    ...
    with objn do
      // statement
In this case, each variable reference or method name in statement is interpreted, if possible, as a member of objn;
otherwise it is interpreted, if possible, as a member of objn1; and so forth. The same rule applies to interpreting the
objs themselves, so that, for instance, if objn is a member of both obj1 and obj2, it is interpreted as obj2.objn.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   50   51   52   53   54   55   56   57   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin