Re: OnFilterRecord



UN wrote:
Thank you, the debugger accepts the following code, but I think it is a bit clumsy ...
In this procedure some addresses are searched (OnFilterRecord:=FindAddress;). When entering it there may be a filter on, either FindName or FindNames. At the end this original filter have to be set on and its name is saved in a variable sFilter.
..
var sFilter :string;
ok :boolean;
bok :TBookmarkStr;
FilterMeth :TFilterRecordEvent;


begin
With aTable do begin
sFilter:='';
try
If filtered then begin
if Assigned(OnFilterRecord) then begin
FilterMeth:=FindName;
if @OnFilterRecord = @FilterMeth then sFilter:='FindName' else begin
FilterMeth:=FindNames;
if @OnFilterRecord = @FilterMeth then sFilter:='FindNames';
end;
end;
end;
bok:=Bookmark;
OnFilterRecord:=FindAddress;
filtered:=true;
FindAddress(aTable,ok);
finally
filtered:=false;
If sFilter<>'' then begin
if sFilter='FindName' then OnFilterRecord:=FindName
else if sFilter='FindNames' then OnFilterRecord:=FindNames;
filtered:=true;
end;
Bookmark:=bok;
end;
end;
end;

It strikes me that you don't really *need* to know which filter method was assigned. You only need to be able to restore whatever method was there originally, regardless of whic specific method it was. That's much easier, and the code looks nicer, too.


var
  ok: Boolean;
  bok: TBookmarkStr;
  OldFilter: TFilterRecordEvent;
begin
  bok := ATable.Bookmark;
  try
    if ATable.Filtered then OldFilter := ATable.OnFilterRecord
    else OldFilter := nil;
    try
      ATable.OnFilterRecord := FindAddress;
      ATable.Filtered := True;
      FindAddress(ATable, OK);
      ATable.Filtered := Assigned(OldFilter);
    finally
      ATable.OnFilterRecord := OldFilter;
    end;
  finally
    ATable.Bookmark := bok;
  end;
end;

There must be an awful lot of side effects in that code, because on the face of it, it doesn't look like it does anything at all.

--
Rob
.