Avatar of DaFou

asked on 

event binding/chaining

Hi All,

I have 3 classes. class A, B and C. with all 3 classes having the event OnSomethingHappend.
A.OnSomethingHappend, B.OnSomethingHappend and C.OnSomethingHappend

C had a member of type B
B has a member of type A


How can i chain the event OnSomethingHappend from A all the way to C via B
So that if the event fires in a it is automaticly bubled up to C and fires there aswell.

I want to bind the events somehow rather then passing on the event.
S0 what i dont want to do is create an event handler in class C that responds to the event in class B that is fired when the event in class A happens.

So code i dont want to use is like this for
class C
B.OnSomethingHappend += new B.SomethingHappend(_B_OnSomethingHappend);

void _B_OnSomethingHappend(object sender, EventArgs eventArgs)
            //do stuff when event in class A fired

Class B
A.OnSomethingHappend += new A.SomethingHappend(_A_OnSomethingHappend);

void _A_OnSomethingHappend(object sender, EventArgs eventArgs)
            OnSomethingHappend(this, new EventArgs());

Is there a more cleaner way to bind/chain events?

Avatar of undefined
Last Comment
Avatar of Adecus

If you want to use events, you have to hook them up with the += operator. I think the code is ugly too. The best way to chain up events in my mind is through a factory. Make a factory who knows about A, B, and C and how they should be chained up. That factory's job then will be to create A, B, and C, and hook their events up in the proper order. This means that A, B, and C must have public events exposed of course.

Your factory might look something like:

public class ABCFactory
    public ABCFactory()

   public A create()
    A = new A();
    B = new B();
    C = new C();
    A.eventName += B.Handler //In whatever order you wanted to fire the events
    B.eventName += C.Handler

   return A;


Now only the factory knows about A, B, and C's events. A does not depend on B and B does not depend on C. This method is much cleaner and decouples A, B, and C nicely.

Avatar of DaFou


Perhaps I am mistaken but does using the ABC factory mean that whenever I have an additional class ( e.g. class D ) that wants to handle class B's event OnSomethingHappend ( that was trigered by the fireing of class A's event OnSomethingHappend ) I have to add class D to the class ABC(D)Factory?

If I am not mistaken this looks not so expandable. I want classes ( D, E, F etc etc ) to respond to class B's event that fires when class A's event fires. but without having to handle class A's event in class B and fire its own event from within class B. Hence the term chaining.

Also I tried to bind B.Handler to A.event like you suggest but I get a compile error stating that conversion from B.Handler to A.Handler is not possible.

any thoughts?
Avatar of DaFou


ignore this, i was mistaken:
"Also I tried to bind B.Handler to A.event like you suggest but I get a compile error stating that conversion from B.Handler to A.Handler is not possible."

the rest of my remarks still stand
Avatar of Adecus

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of DaFou


I did not find what I was looking for, however I do beleive you offered me the best compromise...


C# is an object-oriented programming language created in conjunction with Microsoft’s .NET framework. Compilation is usually done into the Microsoft Intermediate Language (MSIL), which is then JIT-compiled to native code (and cached) during execution in the Common Language Runtime (CLR).

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo