The Delphi Language, Part 2 - Events
(Page 9 of 14 )
The Delphi language supports two different kinds of events: singleton and multicast.
Singleton events have been in the Delphi language since the beginning. They are declared as a property whose type is a procedure type with read and write accessors. Singleton events may have zero or one event listeners. The assignment operator is used to hook a listener to the event, and nil is assigned to remove the listener from the event. Listing 5.3 provides a demonstration of the declaration and use of a singleton event.
Listing 5.3 Singleton Event Demonstration
1: program singleevent;
2:
3: {$APPTYPE CONSOLE}
4:
5: type
6: TMyEvent = procedure (Sender: TObject; Msg: string) of object;
7:
8: TClassWithEvent = class
9: private
10: FAnEvent: TMyEvent;
11: public
12: procedure FireEvent;
13: property AnEvent: TMyEvent read FAnEvent write FAnEvent;
14: end;
15:
16: TListener = class
17: procedure EventHandler(Sender: TObject; Msg: string);
18: end;
19:
20: { TClassWithEvent }
21:
22: procedure TClassWithEvent.FireEvent;
23: begin
24: if Assigned(FAnEvent) then
25: FAnEvent(Self, '*singleton event*');
26: end;
27:
28: { TListener }
29:
30: procedure TListener.EventHandler(Sender: TObject; Msg: string);
31: begin
32: WriteLn('Event was fired. Message is: ', Msg);
33: end;
34:
35: var
36: L: TListener;
37: CWE: TClassWithEvent;
38: begin
39: L := TListener.Create; // create objects
40: CWE := TClassWithEvent.Create;
41: CWE.AnEvent := L.EventHandler; // assign event handler
42: CWE.FireEvent; // cause event to fire
43: CWE.AnEvent := nil; // disconnect event handler
44: ReadLn;n
45: end.
The output of the program shown in Listing 5.3 is
Event was fired. Message is: *singleton event*
Multicast events have been added to the language to support .NET's capability of having multiple listeners for a given event. A multicast event is a property whose type is a procedure type and requires both add and remove accessors. Multicast events can have any number of listeners. The Include() and Exclude() procedures are used to add and remove listeners from a multicast event.
Listing 5.4 provides an example of declaring and using a multicast event.
Listing 5.4 Multicast Event Demonstration
1: program multievent;
2:
3: {$APPTYPE CONSOLE}
4:
5: uses
6: SysUtils;
7:
8: type
9: TMyEvent = procedure (Sender: TObject; Msg: string) of object;
10:
11: TClassWithEvent = class
12: private
13: FAnEvent: TMyEvent;
14: public
15: procedure FireEvent;
16: property AnEvent: TMyEvent add FAnEvent remove FAnEvent;
17: end;
18:
19: TListener = class
20: procedure EventHandler(Sender: TObject; Msg: string);
21: end;
22:
23: { TClassWithEvent }
24:
25: procedure TClassWithEvent.FireEvent;
26: begin
27: if Assigned(FAnEvent) then
28: FAnEvent(Self, '*multicast event*');
29: end;
30:
31: { TListener }
32:
33: procedure TListener.EventHandler(Sender: TObject; Msg: string);
34: begin
35: WriteLn('Event was fired. Message is: ', Msg);
36: end;
37:
38: var
39: L1, L2: TListener;
40: CWE: TClassWithEvent;
41: begin
42: L1 := TListener.Create; // create objects
43: L2 := TListener.Create;
44: CWE := TClassWithEvent.Create;
45: Include(CWE.AnEvent, L1.EventHandler); // assign event handler
46: Include(CWE.AnEvent, L2.EventHandler); // assign event handler
47: CWE.FireEvent; // cause event to fire
48: Exclude(CWE.AnEvent, L1.EventHandler); // disconnect event handler
49: Exclude(CWE.AnEvent, L2.EventHandler); // disconnect event handler
50: ReadLn;
51: end
The output of the program shown in Listing 5.4 is
Event was fired. Message is: *multicast event* Event was fired. Message is: *multicast event*
Note that attempts to use Include() to add the same method more than once to the listener list will result in the method being called multiple times.
In order to maintain compatibility with other .NET CLR languages, the Delphi compiler will implement multicast semantics even for singleton events, creating add and remove accessors for the singleton event. In this implementation, the add() method will result in an overwrite of the existing value.
This chapter is from Delphi for .NET Developer's Guide, by Xavier Pacheco (Sams, 2004, ISBN: 0-672-32443-1). Check it out at your favorite bookstore today.
Buy this book now. |
Next: Visibility Specifiers, Friend Classes and Class Helpers >>
More .NET Articles
More By Xavier Pacheco