I have been working on C# for a long time, but today I encountered a scenario that I thought was very obvious yet I discovered that was not the case.
I need to cascade an event from one class to another. Although the scenario was simple I ran into something that seemed very obvious to me but C# didn't support my “obvious”.
This article is related to my learning from my "obvious” scenario.
In order to understand the situation let's create a scenario. 
- ClassMain calls an Action method in ClassA
- ClassA does some processing and then calls an Action method on ClassB
- The Action method of ClassB does some long-running processing and wants to intermediately tell ClassMain about its progress. It raises an event “UpdateProgressEventB” to share its progress with anyone listening.
- ClassA receives the UpdateProgressEventB from ClassB and then raises an UpdateProgressEventA event.
- ClassMain receives the UpdateProgressEventA event and does some processing.
Simple job, I said to myself and started coding.
I created my classes as in the following.
- public class ClassMain
- {
- readonly ClassA _classA;
- public ClassMain()
- {
- _classA = new ClassA();
- _classA.UpdateProgressA += OnUpdateProgress;
- }
- public void Action()
- {
- _classA.Action();
- }
- private void OnUpdateProgress(string str)
- {
- MessageBox.Show(str);
- }
- }
- public class ClassA
- {
- public Action<string> UpdateProgressA;
- readonly ClassB _classB;
- public ClassA()
- {
- _classB = new ClassB();
- _classB.UpdateProgressB += UpdateProgressA;
- }
- public void Action()
- {
- _classB.Action();
- }
- }
- public class ClassB
- {
- public Action<string> UpdateProgressB;
- public void Action()
- {
- // Do some Processing
- // Update Progress
- if (UpdateProgressB != null)
- {
- UpdateProgressB("5 lines processed");
- }
- // Do some processing
- }
- }
- _classB.UpdateProgressB += UpdateProgressA;
Surprisingly I was wrong, on calling the Action method of ClassMain, I did not receive a Message Box.
On further investigation I found the problem. The problem was in this line:
- _classB.UpdateProgressB += UpdateProgressA;
- public ClassA()
- {
- _classB = new ClassB();
- _classB.UpdateProgressB += OnUpdateProgressB;
- }
- public void OnUpdateProgressB(string str)
- {
- if (UpdateProgressA != null)
- {
- UpdateProgressA(str);
- }
- }
An event handler is added to UpdateProgressB in ClassA. The event handler (OnUpdateProgressB) when called will raise the UpdateProgressA event that will then be used by ClassMain.
Class A after the changes looks like this.
- public class ClassA
- {
- public Action<string> UpdateProgressA;
- readonly ClassB _classB;
- public ClassA()
- {
- _classB = new ClassB();
- _classB.UpdateProgressB += OnUpdateProgressB;
- }
- public void OnUpdateProgressB(string str)
- {
- if (UpdateProgressA != null)
- {
- UpdateProgressA(str);
- }
- }
- public void Action()
- {
- _classB.Action();
- }
- }
For cascading events in C#, we cannot add an event directly to the event, we will need to go through Event Handlers. It becomes difficult to catch the error; the compiler does not throw an error in this situation.

Join the conversation! Your thoughts help the community grow.