i guess understanding of way of writing code still limited. try modify solution search generic lists, can not change code in way accepting arbitrary key words 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 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 item in self * * * * * * * * compile error here * * * * * * * * * * * * * * * * * ! ! ! ! ! ! if afound(item, asearchvalue) 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);
the type defined in code tpredicate. went on use tpredicate<t> type defined in sysutils. replace
tpredicate<t> with
tpredicate in code, , compile.
having said that, simpler if used code in answer accepted. there's no need 2 parameter predicate since variable capture used provide search string.
Comments
Post a Comment