<p>Am 25.05.2016 13:18 schrieb "Michael Van Canneyt" <<a href="mailto:michael@freepascal.org">michael@freepascal.org</a>>:<br>
><br>
><br>
><br>
> On Wed, 25 May 2016, Graeme Geldenhuys wrote:<br>
><br>
>> Hi,<br>
>><br>
>> Can I safely assign a method from an Interface reference to an event<br>
>> handler. The code below works, but I'm not sure if it is safe to do so,<br>
>> and allowed.<br>
>><br>
>> ==================================<br>
>> TTestApp = class(TObject)<br>
>> private<br>
>> rpt: TFPReportReport;<br>
>> FReportFilter: IFPReportFilter; // can hold any export filter<br>
>> ...<br>
>><br>
>> constructor TTestApp.Create;<br>
>> begin<br>
>> rpt := TFPReportReport.Create(nil);<br>
>> rpt.Author := 'Graeme Geldenhuys';<br>
>> rpt.Title := 'FPReport Demo 4 - Frames and Fonts';<br>
>><br>
>> FReportFilter := TFPReportExportPDF.Create; // PDF output<br>
>> //FReportFilter := TFPReportExportAggPas.Create; // PNG output<br>
>> rpt.OnRenderReport := @FReportFilter.RenderReport;<br>
>> ...<br>
>> end;<br>
><br>
><br>
> I am not sure this is safe to do, since an event handler exists of 2<br>
> pointers: data and method. As far as I know, an interface does not have a<br>
> data pointer.</p>
<p>An method pointer is always automatically constructed by the compiler consisting of the address of the method (here IFPReportFilter.RenderReport) and the instance that method belongs to (in this case the FReportFilter), so this will indeed work without problems.<br>
The question however is whether it is guaranteed to keep working and isn't merely the use of an implementation detail...</p>
<p>Regards,<br>
Sven</p>