/*****************************************************************************/ Beispiel zur Verwendung einer Collection in Delphi /*****************************************************************************/ 1. Klassen deklarieren type TListItem = class (TCollectionItem) private FID: integer; FValue: string; public constructor create(Collection: TCollection); override; property ID: integer read FID write FID; property Value: integer read FValue write FValue; //.. beliebig weitere properties end; TListe = class (TCollection) private function GetItems(index: integer): TListItem; procedure SetItems(index: integer; Value: TListItem); public constructor create; function Add: TListItem; property Items[index: integer]: TListItem read GetItems write SetItems; end; implementation { TListe } function TListe.Add: TFieldListItem; begin Result := TListItem(inherited Add); end; constructor TListe.create; begin inherited create(TListItem); end; function TListe.GetItems(index: integer): TListItem; begin Result := TListItem(inherited Items[Index]); end; procedure TListe.SetItems(index: integer; Value: TListItem); begin Items[Index].Assign(Value); end; { TListItem } constructor TListItem.create(Collection: TCollection); begin if assigned(Collection) and (Collection is TListe) then inherited Create(Collection); end; 2. später im Code.. var Liste1: TListe; procedure Form.OnCreate; begin Liste1 := TListe.Create; end; procedure Form.OnDestroy; begin Liste1.Free; end; procedure Form.Test; begin //... Liste1.Add; Liste1.Items[Liste1.Count-1].ID := 5; // der ID Wert eben Liste1.Items[Liste1.Count-1].Value := 'wert'; // ein String //... end; |