Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass event from class C through class B to class A

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

Class 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?

like image 273
Robert Lancaster Avatar asked Apr 03 '12 09:04

Robert Lancaster


1 Answers

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; }
    }
}
like image 87
thecoop Avatar answered Sep 16 '22 12:09

thecoop