I guess my understanding of the way of writing code is still very limited. I try to modify the solution from SEARCH GENERIC LISTS, but I can not change the code in a way that he is accepting arbitrary key words as a search parameter
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.
Usage example:
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);