1

I'd like to relay a event. Currently i intercept InnerEvent in the constructor and relay like this:

obj.InnerEvent+=(...)=>{
    if(OuterEvent != null)
        OuterEvent(...);
}

But it looks bad so i find out relay can be done at event level with add/remove keyword like this:

event Handler OuterEvent{
  add{ obj.InnerEvent+=value;}
  remove{ obj.InnerEvent+=value;}
}

But at this point i cannot raise my OuterEvent no more. I know why it is not directly possible (read here) but i cannot figure out a decent way to keep raising my OuterEvent. Should i bind and relay in the constructor as i always did? What's the best technique?

Community
  • 1
  • 1
Mauro Sampietro
  • 2,739
  • 1
  • 24
  • 50

1 Answers1

1

In a sense, OuterEvent does not exist as an event any more when you use the second approach. It is now nothing but a way of subscribing to obj's InnerEvent, and therefore OuterEvent is now effectively the same event as InnerEvent: there is no delegate backing OuterEvent that invokes just the subscribers to that event. The only way to invoke OuterEvent is thus to raise InnerEvent.

The workaround would be to expose a means of raising InnerEvent from outside obj, but I'd consider this bad practice. I'd prefer sticking to the first method if you need to invoke OuterEvent from your outer class as well. In cases where OuterEvent is truly only forwarding InnerEvent, the second approach is quite elegant.

TC.
  • 4,133
  • 3
  • 31
  • 33