2014-05-15 39 views
0

我創建了一個名爲TRecord的類來存儲數據。我創建了另一個包含TRecord類作爲對象列表的類。我使用TRecord在對象列表中添加記錄,然後在完成後將其設置到父類TTableStore.FManyrecords中。在對象中刪除一個分類的TObjectList

我可以檢索列表和COUNT顯示相同數量的記錄,但它不會讓我檢索每個單獨的記錄。

問題是我無法訪問記錄過程/方法,甚至無法定義記錄的檢索。見的僞代碼的最後一行:

TRecord = class(TObject) 
    private 
    FDescription : Variant; 
    FDirectDeposit : Double; 
    public 
    function GetDescription : Variant; 
    function GetDirectDeposit : Double; 
    procedure SetDescription(Value: Variant; DoValidation: Boolean = True); 
    procedure SetDirect(Value: Double; DoValidation: Boolean = True); 
end; 

TTableStore = class(TObject) 
    private 
    FManyRecords : TObjectList ; 
    FTitle2 : Variant; 
    FNormalEarn : Double; 
    public 
    function GetTitle2 : String; 
    function GetNormalEarn : Double; 
    function GetManyRecords: TObjectList; 
    procedure SetManyRecords(Value: TObjectList; DoValidation: Boolean = True); 
    procedure SetTitle2(Value: String; DoValidation: Boolean = True); 
    procedure SetNormalEarn(Value: Double; DoValidation: Boolean = True); 
end; 

private 
    FReportObj : TTableStore; 
    FRecord: TRecord; 
    objectListTemp: TObjectList; 

implementation 
    objectListTemp := TObjectList.Create(false); 
    FRecord := TRecord.create; 

    Frecord.SetDescription… 
    Frecord.SetDirect… 

    objectListTemp.add(FRecord); 

//next... 
//(get next record… until eof.) 

finally 
    FReportObj.SetManyRecords(objectListTemp); 

//===================== Retreival 

    FReportObj : TTableStore; 
    fListOfRecords : TObjectList; 
    FCurrentRecord : TRecord; 

    fListOfRecords := fReportObj.GetManyRecords; 
    fListOfRecords.count // (is ok) 
    FCurrentRecord := fListOfRecords.Items[1]  // ????????? 

的錯誤是TObjList <> TRecord。我是Delphi新手,所以這可能很簡單。我錯過了什麼或不理解?謝謝。

+2

您正在使用XE2;泛型可用。你應該考慮使用'TObjectList ',這會讓你的生活更輕鬆。 –

+2

不僅應該使用泛型,而且要命名類TRecord –

+0

TRecord僅適用於Stackoverflow。名字被改變以保護無辜者。 –

回答

7

您需要將TObject的轉換爲TRecord像這樣的東西:

FCurrentRecord := TRecord(FListOfRecords.Items[1]); 

這是可能的周圍走另外一條路,所以你可以做這樣的事情:

var 
    X: TRecord; 
    Y: TObject; 
begin 
    X := TRecord.Create; 
    Y := X; 
end; 

這是因爲編譯器知道TRecord是從TObject下降的,但是在您的代碼中,編譯器無法知道列表中的TObject實際上是TRecord。

我建議使用泛型而不是TObjectList。這將創建一個你的類型的對象的列表。你可以使用它作爲你的TRecordList。

type 
    TRecordList = TObjectList<TRecord>; 

當你創建它,你應該使用:

FManyRecords := TRecordList.Create; 

要做到這一點,你需要在你的使用條款System.Generics.Collections。

1

一些人建議您使用泛型來解決您的問題,但我認爲使用泛型對此沒有必要,因爲它們可能會損害您的性能,而不會讓metnion他們帶來很多自己的東西,這會增加您的EXE大小和您的應用程序內存使用情況。 相反,如果TObjectList沒有被整合到類中,你可以通過編寫一個索引屬性來訪問你的內部列表項目,就像你要做的那樣。 你這樣做,像這樣:

TRecord = class(TObject) 
private 
    FDescription : Variant; 
    FDirectDeposit : Double; 
public 
    function GetDescription : Variant; 
    function GetDirectDeposit : Double; 
    procedure SetDescription(Value: Variant; DoValidation: Boolean = True); 
    procedure SetDirect(Value: Double; DoValidation: Boolean = True); 
end; 

TTableStore = class(TObject) 
private 
    FManyRecords : TObjectList; 
protected 
    //Used to read record from the internal list which has specific index 
    function GetRecord(index: Integer): TRecord; 
    //Used to write record on the internal list which has psecific index 
    procedure SetRecord(index: Integer; Value: TRecord); 
public 
    //This property alows you to access any Record from your internal list 
    property MyRecord[index: Integer]: TRecord read GetRecord write SetRecord; 
    //Used for adding new records to your internal list 
    procedure AddRecord(MyRecord: TRecord); 
    //Used to remove a record with specific index from your internal list 
    procedure RemoveRecord(index: Integer); 
end; 

implementation 

procedure TTableStore.AddRecord(MyRecord: TRecord); 
begin 
    //We simply call TObjectList.Add method and pass reference to the object 
    //we wish to add to the list 
    FManyRecords.Add(MyRecord); 
end; 

function TTableStore.GetRecord(index: Integer): TRecord; 
begin 
    //We read TObjectList item as we normally would and then we typecast it 
    //to TRecord class as this is our expected result. 
    result := TRecord(FManyRecords[index]); 
end; 

procedure TTableStore.RemoveRecord(index: Integer); 
begin 
    //We simply call TObjectList.delete method passing it a index number of 
    //item we wish to remove 
    FManyRecords.Delete(index); 
end; 

procedure TForm4.Button1Click(Sender: TObject); 
var MyRecord: TRecord; 
    Description: Variant; 
begin 
    Description := 'Test'; 
    //We create our record 
    MyRecord := TRecord.Create; 
    //We set description of our record 
    MyRecord.SetDescription(Description); 
    //We add our record to the internal table 
    MyTableStore.AddRecord(MyRecord); 
    //We check to see if record which is stored in out internal table on 
    //index 0 to see if its description matches our description variable 
    if MyTableStore[0].GetDescription = Description then 
    begin 
     MessageDlg('It works!',mtInformation,[mbOK],0); 
    end; 
    //Change description to list record directly 
    MyTableStore[0].SetDescription('Test2'); 
    //Test again to see if description got properly updated 
    if MyTableStore[0].GetDescription = 'Test2' then 
    begin 
     MessageDlg('It works!',mtInformation,[mbOK],0); 
    end; 
end; 

有了這個,你避免了需要使用任何仿製藥。 Folowing類似的方法,我已經顯示在上面,你可以轉發任何類中的任何屬性或方法(TObjectList實際上是在你的TTableStore類中的類中構建的)

+0

我剛剛在OP原始代碼上創建我的示例。我自己避免使用變體。 – SilverWarior

+0

沒有注意到它。對不起!從這裏刪除我的評論... P.S.看着OP的代碼,我不會在乎性能,因爲有幾件事要解決。 – TLama

相關問題