我想我对编写代码的方式的理解还是很有限的。我尝试从 SEARCH GENERIC LISTS修改解决方案,但我无法以他接受任意关键字作为搜索参数的方式更改代码
unit Unit_TsearchableTList;
interface
uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons,
contnrs, Generics.Collections;
type
TSearchableObjectList<T: class> = class(TObjectList<T>)
public type
TPredicate = reference to function(aItem: T; asearchValue: String): boolean;
public
function search(aFound: TPredicate<T>; asearchValue: String): T;
end;
implementation
function TSearchableObjectList<T>.search(aFound: TPredicate<T>;
asearchValue: String): T;
var
item: T;
begin
for item in Self do
* * * * * * * * COMPILE ERROR IS HERE * * * * * * * * * * * * * * * *
* ! ! ! ! ! !
if aFound(item, asearchValue) then
Exit(item);
Result := nil;
end;
end.
使用示例:
type
TReplaceElementNames = class
FindName: String;
ReplaceName: String;
ReplacementCondition: TReplacementCondition; // not relevant code
end;
var
LookUpList: TList<TReplaceElementNames>;
search : TReplaceElementNames;
begin
LookUpList := TSearchableObjectList<TReplaceElementNames>.Create;
search := LookUpList.search(
function(aItem: TReplaceElementNames; searchname: String): boolean
begin
Result := aItem.FindName = searchname;
end);