문제

I have this nested function and I want to modify SearchName2 to receive "S" as a parameter as a possible solution to the problem described in here.

I am still learning Delphi and I'd appreciate any help, I've been searching for nested function examples and I can't find anything similar.

PFI = ^TFI;           
TFI = record
    Id         : TToken;
    Name       : TName;
    Parameters : string;
end;

function TListFI.IsIn(S: PChar): PFI;

  function SearchName2(Item: PFI):Boolean;
  var N1, N2: PChar;
  begin
    N1:= StrNew(Item^.Name);
    N2:= StrNew(S);
    SearchName2:= (StrComp(StrUpper(N1), StrUpper(N2)) = 0);
    StrDispose(N1);
    StrDispose(N2);
  end;

begin
  IsIn:= PFI(FirstThat(@SearchName2));
end;
도움이 되었습니까?

해결책

Rob Kennedy gave you the solution in the other discussion:

make SearchName2 be a two-argument function, and then write FirstThat to accept S as a parameter that it can forward to the function argument.

You need to do what Rob told you to do, that is the solution, eg:

type
  TFuncionColeccion = function (Elemento: TObject; S: PChar): Boolean;

function TColeccion.FirstThat (Rutina: TFuncionColeccion; S: PChar): TObject;
var
  i: Integer;
begin
  For i:=0 to Count-1 do
    if Rutina(Items[i], S) then
    begin
      FirstThat:=Items[i];
      exit;
    end;
  FirstThat:=nil;
end;

function SearchName2(Item: PFI; S: PChar):Boolean;
var
  N1, N2: PChar;
begin
  N1 := StrNew(Item^.Name);
  N2 := StrNew(S);
  SearchName2 := (StrComp(StrUpper(N1), StrUpper(N2)) = 0);
  StrDispose(N1);
  StrDispose(N2);
end;

function TListFI.IsIn(S: PChar): PFI;
egin
  IsIn := PFI(FirstThat(@SearchName2, S));
end;

With that said, you can get rid of the use of StrNew() and StrDispose() completely:

function SearchName2(Item: PFI; S: PChar):Boolean;
begin
  SearchName2 := (StrIComp(Item^.Name, S) = 0);
end;
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top