![]() |
Re: Array of TStringList erzeugen
Zitat:
|
Re: Array of TStringList erzeugen
Zitat:
|
Re: Array of TStringList erzeugen
Delphi-Quellcode:
type
TMyClass = class(TObjectList) private function GetItem(AIndex: Integer): TStringList; protected public procedure NewClass; property Items[AIndex: Integer]: TStringList read GetItem; end; function TMyClass.GetItem(AIndex: Integer): TStringList; begin if AIndex<Count then Result := TStringlist(inherited Items[AIndex]) else Result := nil; end; procedure TMyClass.NewClass; begin Add (TStringList.Create); end; procedure Test; // Vor.: MyClass ist created begin myClass.NewClass; MyClass.Items[0].Add ('Hallo'); end; |
Re: Array of TStringList erzeugen
Öhm???
Also von vorn: Eine TObjectList ist eine Liste! Dieser Liste können Objecte vom Typ TObject hinzugefügt werden. Da alle Klassen von TObject Abgeleitet sind, kannst du jede Klasse da einfach reintun, egal ob das eine TStrings, eine TStringlist oder ein TButton oder Ein TLabel oder oder oder... ist, dies alles past in eine einzige Inztanz... Hinzugefügt werden sie einfach mit Add... So das raushollen ist ein wenig schwieriger, gerade dann, wenn man TLabels, TButtons, TForms und Co darin hat, aber dafür gibts ja abfragen ala
Delphi-Quellcode:
Eigendlich ist das einfachste OOP ...
If ... Is TButton Then ... := As TButton;
Schau doch einfach nochmal eine Seite zurück auf mein Beispiel, mit dem TMaloObj und der TMaloObjList, da ist TMaloObj ein Object (irgendeins) und TMaloObjList ist die Liste zu diesem Object... Wenn du die paar ZEilen dort vom Beispiel verstanden hast, kannst du dir ja mal die dpCollection anschauen, denn es bringt nach meiner Meinung nix, was zu benutzen, was man nicht versteht... Bye |
Re: Array of TStringList erzeugen
Zitat:
@Kedariodakon: Es geht nicht um die dpCollection, sondern eher um die technik des templates. Damit kann man nämlich mit 4 zeilen code eine komplette typisierte objektliste erzeugen. |
Re: Array of TStringList erzeugen
Zitat:
Bye |
Re: Array of TStringList erzeugen
Zitat:
TCollection schluckt nur TCollectionItem-Nachfahren! Will man Objekte verwalten wäre eine TObjectList ein passabler Anfang. ;) Und wen so ein Template interessiert... Nix einfacher als das. ;)
Nix weltbewegendes, auf kein Fall etwas schönes.... aber tierisch praktisch :mrgreen:
Delphi-Quellcode:
Die Verwendung ist denkbar einfach (die 4 Zeilen, von denen maximov sprach ;) )
{$IFNDEF TYPED_OBJECT_LIST_TEMPLATE}
unit TypedObjectList_template; interface uses Contnrs; type _OBJECT_LIST_ITEM_ = TObject; {$ENDIF TYPED_OBJECT_LIST_TEMPLATE} {$IFNDEF TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS} // Jedi's include to evaluate compiler version... {$INCLUDE Jedi.inc} type _OBJECT_LIST_IENUM_ = interface; _OBJECT_LIST_ENUM_ = class; _OBJECT_LIST_ = class(TObjectList) protected function GetItem(Index: Integer): _OBJECT_LIST_ITEM_; virtual; procedure SetItem(Index: Integer; const Value: _OBJECT_LIST_ITEM_); virtual; public function Add(aObject: _OBJECT_LIST_ITEM_): Integer; reintroduce; virtual; function Extract(Item: _OBJECT_LIST_ITEM_): _OBJECT_LIST_ITEM_; reintroduce; virtual; function Remove(aObject: _OBJECT_LIST_ITEM_): Integer; reintroduce; virtual; function IndexOf(aObject: _OBJECT_LIST_ITEM_): Integer; reintroduce; virtual; procedure Insert(Index: Integer; aObject: _OBJECT_LIST_ITEM_); reintroduce; virtual; function First: _OBJECT_LIST_ITEM_; reintroduce; function Last: _OBJECT_LIST_ITEM_; reintroduce; property Items[Index: Integer]: _OBJECT_LIST_ITEM_ read GetItem write SetItem; default; function GetIEnumerator(): _OBJECT_LIST_IENUM_; {$IFDEF COMPILER9_UP} function GetEnumerator(): _OBJECT_LIST_ENUM_; {$ENDIF COMPILER9_UP} end; _OBJECT_LIST_IENUM_ = interface function MoveNext: Boolean; function GetCurrent: _OBJECT_LIST_ITEM_; property Current: _OBJECT_LIST_ITEM_ read GetCurrent; end; _OBJECT_LIST_ENUM_ = class(TInterfacedObject, _OBJECT_LIST_IENUM_) private fCollection: _OBJECT_LIST_; fIndex: Integer; constructor Create(List: _OBJECT_LIST_); public function MoveNext: Boolean; function GetCurrent: _OBJECT_LIST_ITEM_; property Current: _OBJECT_LIST_ITEM_ read GetCurrent; end; {$ENDIF TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS} {$IFNDEF TYPED_OBJECT_LIST_TEMPLATE} implementation {$DEFINE TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS} {$ENDIF TYPED_OBJECT_LIST_TEMPLATE} {$IFDEF TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS} { _OBJECT_LIST_ITEM_ } function _OBJECT_LIST_.Add(aObject: _OBJECT_LIST_ITEM_): Integer; begin RESULT := inherited Add(aObject); end; function _OBJECT_LIST_.Extract(Item: _OBJECT_LIST_ITEM_): _OBJECT_LIST_ITEM_; begin RESULT := _OBJECT_LIST_ITEM_(inherited Extract(Item)); end; function _OBJECT_LIST_.First: _OBJECT_LIST_ITEM_; begin RESULT := _OBJECT_LIST_ITEM_(inherited First()); end; function _OBJECT_LIST_.GetItem(Index: Integer): _OBJECT_LIST_ITEM_; begin RESULT := _OBJECT_LIST_ITEM_(inherited Items[Index]); end; function _OBJECT_LIST_.IndexOf(aObject: _OBJECT_LIST_ITEM_): Integer; begin RESULT := inherited IndexOf(aObject); end; procedure _OBJECT_LIST_.Insert(Index: Integer; aObject: _OBJECT_LIST_ITEM_); begin inherited Insert(Index, aObject); end; function _OBJECT_LIST_.Last: _OBJECT_LIST_ITEM_; begin RESULT := _OBJECT_LIST_ITEM_(inherited Last()); end; function _OBJECT_LIST_.Remove(aObject: _OBJECT_LIST_ITEM_): Integer; begin RESULT := inherited Remove(aObject); end; procedure _OBJECT_LIST_.SetItem(Index: Integer; const Value: _OBJECT_LIST_ITEM_); begin inherited Items[Index] := Value; end; {$IFDEF COMPILER9_UP} function _OBJECT_LIST_.GetEnumerator: _OBJECT_LIST_ENUM_; begin RESULT := _OBJECT_LIST_ENUM_.Create(self); end; function _OBJECT_LIST_.GetIEnumerator: _OBJECT_LIST_IENUM_; begin RESULT := GetEnumerator(); end; {$ELSE} function _OBJECT_LIST_.GetIEnumerator: _OBJECT_LIST_IENUM_; begin RESULT := _OBJECT_LIST_ENUM_.Create(self); end; {$ENDIF COMPILER9_UP} constructor _OBJECT_LIST_ENUM_.Create(List: _OBJECT_LIST_); begin fIndex := -1; fCollection := List; end; function _OBJECT_LIST_ENUM_.GetCurrent: _OBJECT_LIST_ITEM_; begin RESULT := fCollection.GetItem(fIndex); end; function _OBJECT_LIST_ENUM_.MoveNext: Boolean; begin inc(fIndex); RESULT := (fIndex > -1) and (fIndex < fCollection.Count); end; {$WARNINGS off} {$IFNDEF TYPED_OBJECT_LIST_TEMPLATE} end. {$ENDIF TYPED_OBJECT_LIST_TEMPLATE} {$ENDIF TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS} {$DEFINE TYPED_OBJECT_LIST_TEMPLATE_SECOND_PASS}
Delphi-Quellcode:
direkt angewand könnte man vor D2005 so durch sie laufen:
interface
uses Contnrs; /// <- wichtig! type TSomeClass = class private fSomeProperty: Integer; public property SomeProperty: Integer read fSomeProperty write fSomeProperty; end; {$DEFINE TYPED_OBJECT_LIST_TEMPLATE} type _OBJECT_LIST_ITEM_ = TSomeClass; {$INCLUDE TypedObjectList_template.pas} TSomeClassList = _OBJECT_LIST_; implementation {$INCLUDE TypedObjectList_template.pas} end.
Delphi-Quellcode:
In D2005 geht nun auch gleich das hier:
with SomeClassList.GetIEnumerator() do
while MoveNext() do WriteLn(Current.SomeProperty);
Delphi-Quellcode:
var
SomeInstance :TSomeClass; begin for SomeInstance in SomeClassList do WriteLn(SomeInstance.SomeProperty); |
Re: Array of TStringList erzeugen
Wenn das nicht einfach ist - abgesehen davon, dass es hässlich aussieht - dann weiss ich auch nicht!
|
Re: Array of TStringList erzeugen
Zitat:
Nur noch benutzen! :) |
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:45 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz