[fpc-devel] Property overloading vs. property array enumerators
Anthony Walter
sysrpl at gmail.com
Thu Jan 28 16:23:42 CET 2016
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;
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20160128/319a36eb/attachment.html>
More information about the fpc-devel
mailing list