Search code examples
delphieventscopyassign

Delphi - Why events are not copied in Assign() procedures?


I observed several Assign() procedures and noticed that events are generally ignored during the assignment. For example, the TBitmap assignment method does not copy the OnChange event. I want to know

  1. If it's a commonly accepted way to not copy the events during the assignment, i.e. if all users rely on the fact that the events are never - and should never be - copied during an assignation?
  2. Why the events are (at least commonly) not copied during an assignation?
  3. Or perhaps I'm wrong, and the events may perfectly be copied, just depending of the circumstances?

Regards


Solution

  • I'm not aware of a 'rule' that dictates this, but I think you are right and it almost never happens. It's up to the component's author though, because Assign is introduced in TPersistent, but doesn't do anything by itself until you actually implement an override. Actually it throws an exception (X cannot be assigned to Y) by default.

    And that's the power of Assign too. The source you assign from doesn't even have to be the same type of component, because every assignment is a custom implementation. In your TBitmap example, you could assign it to a TPicture, and you might even assign other types of graphics to TBitmap which would let those graphics draw themselves on the bitmap's canvas.

    In most, if not all, cases I've encountered, Assign is about that: an assignment and sometimes even conversion of data (of state, if you like) of one object into another.

    Events are different. It's not up to an object to have an event handler and manage it. It's not that object's job to make the decision that its subscribers also want to subscribe to the other object. The value of/pointer to the event handler is not part of the (relevant) data of the object.

    It just exposes the possibility for others to listen to things that have happened to it, and if those things want to listen to the other object too, they just have to assign an event handler to those objects as well.

    So to me it makes perfect sense that events aren't copied, and I think I've never included events in the Assign implementations that I've written myself.