Delphi 如何声明数组属性?
我构建了班级制度Delphi 如何声明数组属性?,delphi,oop,delphi-7,delphi-xe2,delphi-2010,Delphi,Oop,Delphi 7,Delphi Xe2,Delphi 2010,我构建了班级制度 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 re
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字段中,属性应该是字段列表(我使用了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
是一个(私有)函数,它接受一个整数(索引并返回相应的TFieldSpec
,例如
function TTableSpec.GetField(Index: Integer): TFieldSpec;
begin
result := FFields[Index];
end;
参见。我同意Andreas给出的关于索引属性的答案是海报所寻求的解决方案
为了完整性,对于将来的访问者,我想指出,属性可以有一个数组类型,只要该类型被命名。这同样适用于记录、指针和其他派生类型
type
tMyColorIndex = ( Red, Blue, Green );
tMyColor = array [ tMyColorIndex ] of byte;
tMyThing = class
private
fColor : tMyColor;
public
property Color : tMyColor read fColor;
end;
除非您确定需要100个字段,否则我将创建一个类型,如type TFields=数组of TFieldSpec
,然后将字段属性指定为FFields:TFields
。重命名标题,并删除不相关的代码。我们不需要查看整个单元来确定问题所在。您确定要使用数组类型的属性,或者更确切地说是索引器吗?是的,很抱歉,我没有注意到这一点。(: