Here is a preferable solution:
property Controls: IControlEnumerator read GetItems;
This allows for both:
for C in Test.Controls do ShowMessage(C.Name);
// or
for I := 0 to Test.Controls.Count - 1 do ShowMessage(Test.Controls[I]);
That is you can access the property as either an enumerable collection, or
and an indexed collection. Note it has support for a default indexer
property and a count property.
This is where where:
IControlEnumerator = IIndexedEnumerator<TControl>;
And:
type
IIndexedEnumerator<T> = interface(IEnumerator<T>)
function GetEnumerator: IIndexedEnumerator<T>;
function GetCount: Integer;
function GetItem(Index: Integer): T;
property Count: Integer read GetCount;
property Item[Index: Integer]: T read GetItem; default;
end;
And finally putting together Test object as a simple example. Note that
TTest has full private access to the underlying container FList:
type
TTest = class
private
FList: TList<TControl>;
function GetItems: IControlEnumerator;
public
constructor Create(Controls: array of TControl);
destructor Destroy; override;
property Controls: IControlEnumerator read GetItems;
end;
_______________________________________________
fpc-devel maillist - [email protected]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel