In Part 1, I talked about the two new property access specifiers and showed a way of using them. Now, I would like to show another way of using them.
add_Some combines the existing delegates in FOnSome with the new delegate, and assigns it to FOnSome, such that the new delegate is added to the last position.
remove_Some removes the specified delegate from the existing delegates listed in FOnSome, and assigns the result to FOnSome.
Is there a way of clearing the delegate list, or changing the order of insertion of delegates if you're not privy to the declarations?
To be continued...
program
MulticastEvents2;
{$APPTYPE CONSOLE}
uses
Classes;
type
TSomeEvent =
procedure
;
TSomeObject =
class
private
FOnSome: TSomeEvent;
add_Some(Value: TSomeEvent);
remove_Some(Value: TSomeEvent);
public
ClearSome;
DoSome;
property
OnSome: TSomeEvent add add_Some remove remove_Some;
end
TSomeObject
.
begin
FOnSome := TSomeEvent(Delegate
Combine(Delegate(@FOnSome), Delegate(@Value)));
var
List:
array
of
Delegate;
I:
Integer
if
Assigned(FOnSome)
then
List := Delegate(@FOnSome).GetInvocationList;
for
I :=
0
to
Length(List) -
1
do
Exclude(OnSome, TSomeEvent(List<img src="http:
//chuacw.ath.cx/emoticons/emotion-55.gif" alt="Idea">));
FOnSome;
FOnSome := TSomeEvent(System
Delegate
Remove(Delegate(@FOnSome), Delegate(@Value)));
Some1;
WriteLn
(
'Hello world, I am Some1'
);
Some2;
'Hello world, I am Some2'
Some3;
'Hello world, I am Some3'
SomeObj: TSomeObject;
SomeObj := TSomeObject
Create;
Include(SomeObj
OnSome, Some1);
OnSome, Some2);
OnSome, Some3);
SomeObj
'All handlers will now be removed...'
'End of demonstration for add and remove property access specifiers'
ReadLn;