如何声明数组属性?

use*_*626 10 delphi oop delphi-7 delphi-2010 delphi-xe2

我构建了类系统

  TTableSpec=class(Tobject)
  private
    FName : string;
    FDescription : string;
    FCan_add : Boolean;
    FCan_edit : Boolean;
    FCan_delete : Boolean;
    FFields : array[1..100] of TFieldSpec;
  public
    property Name: String read FName;
    property Description: String read FDescription;
    property Can_add : Boolean read FCan_add;
    property Can_edit : Boolean read FCan_edit;
    property Can_delete : Boolean read FCan_delete;
    property Fields : array read FFields;
  end;
Run Code Online (Sandbox Code Playgroud)

因此,在TableSpec中,Fields属性应该是字段列表(我使用TFieldSpec数组).如何组织字段列表(使用或不使用数组)作为编译的结果我收到错误

[Error] Objects.pas(97): Identifier expected but 'ARRAY' found
[Error] Objects.pas(97): READ or WRITE clause expected, but identifier 'FFields' found
[Error] Objects.pas(98): Type expected but 'END' found
[Hint] Objects.pas(90): Private symbol 'FFields' declared but never used
[Fatal Error] FirstTask.dpr(5): Could not compile used unit 'Objects.pas'
Run Code Online (Sandbox Code Playgroud)

And*_*and 25

你的路线

property Fields : array read FFields;
Run Code Online (Sandbox Code Playgroud)

是无效的语法.它应该是

property Fields[Index: Integer]: TFieldSpec read GetField;
Run Code Online (Sandbox Code Playgroud)

其中GetField是一个(私有)函数,它接受一个整数(Index)并返回相应的TFieldSpec,例如,

function TTableSpec.GetField(Index: Integer): TFieldSpec;
begin
  result := FFields[Index];
end;
Run Code Online (Sandbox Code Playgroud)

请参阅有关数组属性的文档.


Dav*_*ois 11

我同意Andreas给出的关于INDEXED属性的答案是海报正在寻找的解决方案.

为了完整性,对于未来的访问者,我想指出一个属性可以有一个数组类型,只要该类型被命名.这同样适用于记录,指针和其他派生类型.

type
  tMyColorIndex = ( Red, Blue, Green );
  tMyColor = array [ tMyColorIndex ] of byte;

  tMyThing = class
    private
      fColor : tMyColor;
    public
      property Color : tMyColor read fColor;
  end;
Run Code Online (Sandbox Code Playgroud)