How can I declare an array property?

有些话、适合烂在心里 提交于 2019-12-05 02:41:01

Your line

property Fields : array read FFields;

is invalid syntax. It should be

property Fields[Index: Integer]: TFieldSpec read GetField;

where GetField is a (private) function that takes an integer (the Index) and returns the corresponding TFieldSpec, e.g.,

function TTableSpec.GetField(Index: Integer): TFieldSpec;
begin
  result := FFields[Index];
end;

See the documentation on array properties.

I agree the answer regarding INDEXED properties, given by Andreas, is the solution the poster is looking for.

For completeness, for future visitors, I'd like to point out that a property can have an array type, as long as the type is named. The same applies to records, pointers, and other derived types.

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

  tMyThing = class
    private
      fColor : tMyColor;
    public
      property Color : tMyColor read fColor;
  end;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!