Search code examples
delphieventstdataset

How to assign TFDMemtable AfterPost and AfterDelete events on runtime?


I have a DataModule shared by several forms and in that I built a procedure to processing a TFDMemtable passed as parameter. In order to process it I must to disable the events AfterPost and AfterDelete and when conclude processing I have to enable them back. I'm not suceeding in enable them back, because I can't get the actual name of these events in a form "actualnameAfterpost".

I've tried :

    pMemTable.AfterPost  := @pMemTable.AfterPost ; // Result ==> compile error          
    pMemTable.AfterPost  := addr(pMemTable.AfterPost) ; // Result ==> compile error          
   pMemTable.AfterPost  := MethodAddress(Pmemtable.ClassName +'AfterPost'); // 
   Result ==> compile error

This is the main code :

procedure UpdateMemtable (var pMemTable : TFDmemtable);
begin
      pMemTable.AfterPost   := nil;
      pMemTable.AfterDelete := nil;

     TRY
     with pMemTable do 
     begin 
             { code to process pMemtable }
        end;
      FINALLY
        pMemTable.AfterPost    := ["actualmemtablenameAfterPost" ??];   
        pMemTable.AfterDelete  := ["actualmemtablenameAfterDelete" ??];   
      END;    
end;

Thanks all !


Solution

  • Both events are of type TDataSetNotifyEvent. Use two local variables of this type to hold the events temporarily.

    To disable the events, save them to the temporary variables and then nil them. After you have done the manipulation restore the actual events from the temporary vars.

    procedure UpdateMemtable (var pMemTable : TFDmemtable);
    var
      tmpAfterPost,
      tmpAfterDelete: TDataSetNotifyEvent
    begin
      tmpAfterPost := pMemTable.AfterPost;
      tmpAfterDelete := pMemTable.AfterDelete;
    
      pMemTable.AfterPost   := nil;
      pMemTable.AfterDelete := nil;
    
      TRY
        with pMemTable do 
        begin 
                 { code to process pMemtable }
        end;
      FINALLY
        pMemTable.AfterPost    := tmpAfterPost;   
        pMemTable.AfterDelete  := tmpAfterDelete;   
      END;    
    end;