Alex Rivera | Logout

Pass event from class C through class B to class A

Asked 2012-04-03T09:40:39.677
14

I have Class A which implements a large number of instances of Class B. Class B encapsulates an instance of Class C.

Class C raises events which need to be handled by Class A. Class A does not need to know about Class C. Class C is passing back performance based statistics which A then needs to coalesce.

How do I create the events in Class B and connect them so that Class A can subscribe to Class B's events and receive the events from Class C?

Edit
Report

1 Answer

29

An event is nothing more than a pair of methods wrapping a delegate field. You can override the default implementation of the add & remove methods in ClassB to pass the value straight to the event in ClassC:

public class ClassB
{
    private ClassC m_C = new ClassC();

    public event EventHandler MyEvent
    {
        add { m_C.MyEvent += value; }
        remove { m_C.MyEvent -= value; }
    }
}
answered 2012-04-03T09:43:49.543

Your Answer