gpt4 book ai didi

delphi - 使用搜索字符串搜索通用列表,错误 E2034 参数过多

转载 作者:行者123 更新时间:2023-12-03 15:54:48 25 4
gpt4 key购买 nike

我想我对代码编写方式的理解还是很有限的。我尝试修改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);

最佳答案

您在代码中定义的类型是 TPredicate 。但你随后继续使用TPredicate<T>这是 SysUtils 中定义的类型。只需更换即可

TPredicate<T>

TPredicate

在你的代码中,它将编译。

<小时/>

话虽如此,如果您使用接受的答案中的代码会更简单。由于使用变量捕获来提供搜索字符串,因此不需要两个参数谓词。

关于delphi - 使用搜索字符串搜索通用列表,错误 E2034 参数过多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17745408/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com