2012-10-13 42 views
9

我構造類系統我該如何聲明一個數組屬性?

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屬性應的字段的列表(I使用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' 
+0

除非你確信你將需要整整100場,我想創建一個像類型'型中tfields = TFieldSpec'的數組,然後指定字段屬性爲'FFields:TFields'。 –

+0

重命名標題,並刪除不相關的代碼。我們無需查看整個單元,以便在此查明問題。 –

+0

您確定要使用數組類型的屬性,還是索引器? – CodesInChaos

回答

21

你行

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

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;