Search the Community
Showing results for tags 'anonymous methods'.
Found 2 results
The usual way of creating an event handler in Delphi is to double-click the right-hand field in the Events tab of the Object Inspector. This creates a method with the proper signature; when the form is loaded, the OnXyzEvent is assigned the method name: FOnXyzEvent := aForm.XyzEvent; In theory, it should be possible to do something like this for a typical event handler: FOnXyzEvent := procedure(Sender: TObject) begin doSomething; end; However, I have a TpgScript object that has an AfterExecute event with this signature: procedure TpgScriptEx.DoOnAfterExecute(Sender: TObject; SQL: string); begin // end; FScript.AfterExecute := DoOnAfterExecute; // this works fine // but for this, the compiler complains: // [dcc32 Error] myfile.pas(1258): E2010 Incompatible types: 'TAfterStatementExecuteEvent' and 'Procedure' FScript.AfterExecute := procedure (Sender: TObject; SQL: string) begin // end; If I try casting the procedure, I get an Invalid Cast error. This seems like it should be a compiler bug. Is there a workaround for this?
Code that looks correct, and appear to compile alright, but which doesn't execute well. Can you spot the error? See my blog post for a link to a SCCE. var Handler: THandlerClass; hType: THandlers; begin Broker.Clear; for hType in [foo, bar] do begin case hType of foo: Handler := TFoo.Create; bar: Handler := TBar.Create; end; Broker.AddHandler(Handler.OnHandle); end; end; https://larsfosdal.blog/2019/02/08/delphi-pitfalls-of-anonymous-methods-and-capture/