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ə176/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   172   173   174   175   176   177   178   179   ...   294
DelphiLanguageGuide

Array Properties
Array properties are indexed properties. They can represent things like items in a list, child controls of a control, and
pixels of a bitmap.
The declaration of an array property includes a parameter list that specifies the names and types of the indexes. For
example,
property Objects[Index: Integer]: TObject read GetObject write SetObject; 
property Pixels[X, Y: Integer]: TColor read GetPixel write SetPixel; 
property Values[const Name: string]: string read GetValue write SetValue;
The format of an index parameter list is the same as that of a procedure's or function's parameter list, except that
the parameter declarations are enclosed in brackets instead of parentheses. Unlike arrays, which can use only
ordinal-type indexes, array properties allow indexes of any type.
For array properties, access specifiers must list methods rather than fields. The method in a read specifier must be
a function that takes the number and type of parameters listed in the property's index parameter list, in the same
order, and whose result type is identical to the property's type. The method in a write specifier must be a procedure
that takes the number and type of parameters listed in the property's index parameter list, in the same order, plus
an additional value or const parameter of the same type as the property.
For example, the access methods for the array properties above might be declared as
function GetObject(Index: Integer): TObject; 
function GetPixel(X, Y: Integer): TColor; 
function GetValue(const Name: string): string; 
procedure SetObject(Index: Integer; Value: TObject); 
procedure SetPixel(X, Y: Integer; Value: TColor); 
procedure SetValue(const Name, Value: string);
An array property is accessed by indexing the property identifier. For example, the statements
if Collection.Objects[0] = nil then Exit; 
152


Canvas.Pixels[10, 20] := clRed; 
Params.Values['PATH'] := 'C:\BIN';
correspond to
if Collection.GetObject(0) = nil then Exit; 
Canvas.SetPixel(10, 20, clRed); 
Params.SetValue('PATH', 'C:\BIN');
The definition of an array property can be followed by the default directive, in which case the array property becomes
the default property of the class. For example,
type 
   TStringArray = class 
    public 
       property Strings[Index: Integer]: string ...; default; 
          ... 
    end;
If a class has a default property, you can access that property with the abbreviation 
object[index]
, which is
equivalent to 
object.property[index]
. For example, given the declaration above, 
StringArray.Strings
[7]
 can be abbreviated to 
StringArray[7]
. A class can have only one default property. Changing or hiding the
default property in descendant classes may lead to unexpected behavior, since the compiler always binds to
properties statically.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   172   173   174   175   176   177   178   179   ...   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