【发布时间】:2011-05-18 11:57:43
【问题描述】:
我已经定义了一个从 TDictionary 派生的集合,并且需要定义一个自定义枚举器来应用额外的过滤器。
我无法访问 TDictionary FItems 数组(它是私有的),所以我无法定义 MoveNext 方法
您将如何继续在从 TDictionary 派生的类上重新定义过滤枚举器?
这是一个简单的代码来说明我想要做什么:
TMyItem = class(TObject)
public
IsHidden:Boolean; // The enumerator should not return hidden items
end;
TMyCollection<T:TMyItem> = class(TDictionary<integer,T>)
public
function GetEnumerator:TMyEnumerator<T>; // A value filtered enumerator
type
TMyEnumerator = class(TEnumerator<T>)
private
FDictionary: TMyCollection<integer,T>;
FIndex: Integer;
function GetCurrent: T;
protected
function DoGetCurrent: T; override;
function DoMoveNext: Boolean; override;
public
constructor Create(ADictionary: TMyCollection<integer,T>);
property Current: T read GetCurrent;
function MoveNext: Boolean;
end;
end;
function TMyCollection<T>.TMyEnumerator.MoveNext: Boolean;
begin
// In below code, FIndex is not accessible, so I can't move forward until my filter applies
while FIndex < Length(FDictionary.FItems) - 1 do
begin
Inc(FIndex);
if (FDictionary.FItems[FIndex].HashCode <> 0)
and not(FDictionary.FItems[FIndex].IsHidden) then // my filter
Exit(True);
end;
Result := False;
end;
【问题讨论】:
标签: delphi enumerator tdictionary