delphi – 如何声明数组属性?

前端之家收集整理的这篇文章主要介绍了delphi – 如何声明数组属性?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我构建了类系统
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;

因此在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'

解决方法

你的行
property Fields : array read FFields;

是无效的语法.它应该是

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

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

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

the documentation on array properties.

猜你在找的Delphi相关文章