Can I assign the method of an extended record to an event?

Can I assign the method of an extended record to an event?
http://stackoverflow.com/questions/29607117/can-i-assign-the-method-of-an-extended-record-to-an-event-in-delphi

Comments

  1. It works in XE7. No warnings. Method content executes as expected on button click.

    ReplyDelete
  2. I remember this failed in the past. But worked for old turbo Pascal style 'object' types.  be sure to check if the instance pointer is OK though.

    ReplyDelete
  3. The code in in SO question leads to undefined behavior though, no? After all the record won't exist after the procedure exits.

    That said, it should compile...

    ReplyDelete
  4. Of course that's only simplified example code. In my real world application the record is not a local variable but there is a field of the form which is an array of these records. Each record stores some data for multiple controls that are created at runtime and has the event handlers for these controls.

    ReplyDelete
  5. Actually, as long as the code in the event handlers does not access the record's fields, it wouldn't really matter whether the record is still valid. Methods of records are always static and therefore do not access a VMT when called. But of course that would be really bad design.

    ReplyDelete

Post a Comment