Simulating multicast events in Win32 Delphi

In Win32 Delphi OOP, only one method can be assigned as a handler to a component's event. In Delphi for .Net, multiple event handlers can be bound to the same event. Here's a simulation of multicast events in Win32 Delphi.
More of this Feature • Download Source Code

When you develop (Win32) applications using Delphi you almost always write the code to handle some component's event(s). For example, when you drop a TButton on a form and double click it, an empty event handler is created where you write the code to react on the user click action. Win32 Delphi allows you to assign only one method as a handler to an event of a component.

Join the Discussion "Post your views, comments, questions and doubts to this article." Discuss! Related Resources • OOP in Delphi • Simulating class properties in Win32 Delphi • Language features in Delphi for .NET

With Delphi for .NET a notion of multicast events was introduced. A developer can assign two or more methods to be called when an event gets fired. Let's see how to build a Win32 Delphi object that maintains a list of the methods its event(s) is handled by - thus creating a simulation of multicast event handling. Before we start, I strongly suggest that you freshen up your Delphi OOP and custom component development memory... A simple example of multicast event handling in Win32 Delphi First, we'll define a simple class (TMultiEventClass) with only one event of TMutliEvent type. In order to store a list of methods that will be called when event is fired (using the FireMultiEvent) we'll use a fMultiEventHandlers TList object. The TList Delphi class is designed to be used as a storage mechanism for an array of pointers. By design, each method (function or procedure) in Delphi code can be represented with a TMethod Delphi type. A TMethod type can be used, for example, to execute a method by its name.

type   PMethod = ^TMethod;

  TMutliEvent = procedure(const Value : string) of  object;

  TMultiEventClass = class   private     fMultiEventHandlers : TList;   public     constructor Create;     destructor Destroy; override;

    procedure AddMultiEvent(const Value: TMutliEvent);     procedure RemoveMultiEvent(const Value:  TMutliEvent);

    procedure FireMultiEvent;   end;

Related Articles • • • • •

Implementing MultiCast Events for Win32 Delphi's TDataset and TField De... Dynamically set event handlers to Delphi controls NOT being inherited from ... How to Detach an Event Handler from a Delphi Control Event Get the list of events with attached event handlers (Delphi for Win32) Learn about: properties, events and Delphi Pascal - Page 2/2

Guide since 1998 Zarko Gajic Delphi Programming Guide
• • •

Sign up for my Newsletter My Blog My Forum