There are 3 classes A, B, C(Consumer). Class A calls B to fire event so that Class C can receive as it has subscribed. How to achieve this functionality?
Below is the code.
public delegate void TestDelegate();
public class B
{
public event TestDelegate TestEvent;
public B()
{
}
public void Fire()
{
TestEvent();//Null reference exception as not subscribed to the event as TestEvent is always null
}
}
public class A
{
static void Main()
{
B b = new B();
b.Fire(); //Null reference exception as not subscribed to the event.
}
}
//Consumer application
public Class C
{
static void Main()
{
B b = new B();
b.TestEvent+= new TestDelegate(c_TestEvent);
}
static void c_TestEvent()
{
Console.WriteLine("Console 2 Fired");
}
}