Originale-mail to me for new edition

 

Index specifiers

 

Index specifiers allow several properties to share the same access method while representing different values. An index specifier consists of the directive index followed by an integer constant between -2147483647 and 2147483647. If a property has an index specifier, its read and write specifiers must list methods rather than fields. For example,

type

  TRectangle = class

  private

    FCoordinates: array[0..3] of Longint;

    function GetCoordinate(Index: Integer): Longint;

    procedure SetCoordinate(Index: Integer; Value: Longint);

  public

    property Left: Longint index 0 read GetCoordinate write SetCoordinate;

    property Top: Longint index 1 read GetCoordinate write SetCoordinate;

    property Right: Longint index 2 read GetCoordinate write SetCoordinate;

    property Bottom: Longint index 3 read GetCoordinate write SetCoordinate;

    property Coordinates[Index: Integer]: Longint read GetCoordinate write SetCoordinate;

    ...

  end;

An access method for a property with an index specifier must take an extra value parameter of type Integer. For a read function, it must be the last parameter; for a write procedure, it must be the second-to-last parameter (preceding the parameter that specifies the property value). When a program accesses the property, the property’s integer constant is automatically passed to the access method.

Given the declaration above, if Rectangle is of type TRectangle, then

Rectangle.Right := Rectangle.Left + 100;

corresponds to

Rectangle.SetCoordinate(2, Rectangle.GetCoordinate(0) + 100);

 

Topic groups

 

See also

Properties: Overview

Property access

 

 

译文

 

索引说明符

 

索引说明符允许几个属性共享相同的方法而表示不同的值。索引说明符由指示字index和紧随其后的整数常量(范围 -21474836472147483647)组成。如果一个属性有一个索引说明符,那么它的readwrite说明符中都必需列出方法而不是域。例如,

type

  TRectangle = class

  private

    FCoordinates: array[0..3] of Longint;

    function GetCoordinate(Index: Integer): Longint;

    procedure SetCoordinate(Index: Integer; Value: Longint);

  public

    property Left: Longint index 0 read GetCoordinate write SetCoordinate;

    property Top: Longint index 1 read GetCoordinate write SetCoordinate;

    property Right: Longint index 2 read GetCoordinate write SetCoordinate;

    property Bottom: Longint index 3 read GetCoordinate write SetCoordinate;

    property Coordinates[Index: Integer]: Longint read GetCoordinate write SetCoordinate;

    ...

  end;

含有索引说明符的属性,其访问方法必需额外接受一个Integer类型的值参数。对于read函数,该参数必需是最后一个参数;对于write过程,该参数必需是倒数第二个参数(在用于指定属性值的参数之前)。当程序访问属性时,属性的整数常量会被自动传给访问方法。

给出上面的声明,如果Rectangle的类型是TRectangle,那么

Rectangle.Right := Rectangle.Left + 100;

相应的内部处理是

Rectangle.SetCoordinate(2, Rectangle.GetCoordinate(0) + 100);

 

主题组

 

相关主题

属性:概述

属性访问

 

 

编者注

用于索引属性的整数常量,根据原文中所述,范围是 -21474836472147483647。需要注意的是,最小值不是 -2147483648。经过编者考证,当使用 -2147483648时,编译器会报告错误信息“Incompatiable types: ‘Int64’ and ‘Integer’”。