Multicast delegate checking for particular delegate

  • Thread starter Thread starter Edward Diener
  • Start date Start date
E

Edward Diener

I am surprised that a MulticastDelegate does not have a particular member
function which checks if a particular delegate is in its invocation list. I
can do this manually of course but a Find, or something like that, should
have been a member function of the MulticastDelegate.
 
Edward Diener said:
I am surprised that a MulticastDelegate does not have a particular member
function which checks if a particular delegate is in its invocation list. I
can do this manually of course but a Find, or something like that, should
have been a member function of the MulticastDelegate.

I don't think it's the kind of thing that's used very often, to be
honest. I certainly have never needed it myself, and I can't remember
anyone ever asking for it before...
 
Jon said:
I don't think it's the kind of thing that's used very often, to be
honest. I certainly have never needed it myself, and I can't remember
anyone ever asking for it before...

It could be used to make sure that a delegate referencing the same member
function is not added twice or removed if it is not already there. The
former is more important as I assume attempting to remove a delegate which
does not already exist does nothing. However adding the same member function
twice is probably not wise.

It might be argued that any code which could add the same handler twice is
flawed in it design, and that may be so. When you replied I re-checked my
code that I was writing to prevent that and realized that if my logic was
correct, it could not happen by design. However I can imagine situations
where a Find might be useful, or a MulticastDelegate function which only
adds a delegate handler if that delegate handler does not already exist in
its invocation list might be useful.
 
Edward Diener said:
It could be used to make sure that a delegate referencing the same member
function is not added twice or removed if it is not already there. The
former is more important as I assume attempting to remove a delegate which
does not already exist does nothing. However adding the same member function
twice is probably not wise.

Well, in some cases it's not. In some cases it's exactly what you want.
It might be argued that any code which could add the same handler twice is
flawed in it design, and that may be so. When you replied I re-checked my
code that I was writing to prevent that and realized that if my logic was
correct, it could not happen by design. However I can imagine situations
where a Find might be useful, or a MulticastDelegate function which only
adds a delegate handler if that delegate handler does not already exist in
its invocation list might be useful.

The easiest way of doing that is probably just to remove it first, to
be honest.

myDelegate -= foo;
myDelegate += foo;

Admittedly that won't reduce the number if it's been added more than
once already...
 
Back
Top