How to handle an event by a chain of objects?

If you have the following:

public static void main() { MyClass1 obj = new MyClass1(); obj.Method1(); } public class MyClass1() { public void Method1() { MyClass2 obj = new MyClass2(); obj.Method1(); } } public class MyClass2() { public void Method1() { MyClass3 obj = new MyClass3(); obj.Method1(); } } public class MyClass3() { public void Method1() { // Raise event here that is handled in MyClass1? } } 

Can MyClass3.Method1() create an event that is processed in MyClass1 ?

How will the event handling code be written if I want to do this?

+7
source share
5 answers

Yes, it is possible, but since each level does not know about the deeper levels of your chain, you will have to create events in each class. Some people like it:

 public static void main() { MyClass1 obj = new MyClass1(); obj.MyEvent += (s, e) => { Console.WriteLine("Fired!"); }; obj.Method1(); } public class MyClass1 { public void Method1() { MyClass2 obj = new MyClass2(); obj.MyEvent += (s, e) => { OnMyEvent(); }; obj.Method1(); } public event EventHandler MyEvent; private void OnMyEvent() { var myEvent = MyEvent; if (myEvent != null) myEvent(this, EventArgs.Empty); } } public class MyClass2 { public void Method1() { MyClass3 obj = new MyClass3(); obj.MyEvent += (s, e) => { OnMyEvent(); }; obj.Method1(); } public event EventHandler MyEvent; private void OnMyEvent() { var myEvent = MyEvent; if (myEvent != null) myEvent(this, EventArgs.Empty); } } public class MyClass3 { public void Method1() { // Raise event here that is handled in MyClass1? OnMyEvent(); } public event EventHandler MyEvent; private void OnMyEvent() { var myEvent = MyEvent; if (myEvent != null) myEvent(this, EventArgs.Empty); } } 
+3
source

To bind event handlers, use the add / remove syntax in MyClass2. From MyClass1, install SomeEvent and in MyClass3, raise it.

 public class MyClass1 { MyClass2 obj = new MyClass2(); public MyClass1() { obj.SomeEvent += obj_SomeEvent; } public void Method1() { obj.Method1(); } private static void obj_SomeEvent(object sender, EventArgs e) { Console.WriteLine("Some event fired"); } } public class MyClass2() { MyClass3 cls3 = new MyClass3(); public void Method1() { cls3.FireSomeEvent(); } public event MyEventHandler SomeEvent { add { this.cls3.SomeEvent += value; } remove { this.cls3.SomeEvent -= value; } } } public class MyClass3() { public event EventHandler SomeEvent; private void OnSomeEvent() { if (SomeEvent!= null) { SomeEvent(this, new EventArgs()); } } public void FireSomeEvent { OnSomeEvent(); } } 
+2
source

ABC event handling assumes that you have a subscriber and publisher. Therefore, you might want MyClass3 to have a public event, and MyClass1 subscribes to this event.

However, in your specific code, this complexity makes no sense - the easiest way is to simply use the callback function:

 public static void main() { MyClass1 obj = new MyClass1(); obj.Method1(); } public class MyClass1{ public void Method1() { MyClass2 obj = new MyClass2(); obj.Method1(MyEventHandler); } public void MyEventHandler() { //... } } public class MyClass2{ public void Method1(Action callback) { MyClass3 obj = new MyClass3(); obj.Method1(callback); } } public class MyClass3{ public void Method1(Action callback) { // Raise event here that is handled in MyClass1? callback(); } } 
+1
source

You can add an event to the broker class to bind it. Something like that:

 using System; using System.Windows.Forms; namespace Demo { class Program { static void Main(string[] args) { MyClass1 obj = new MyClass1(); obj.Method1(); } } public class MyClass1 { public void Method1() { MyClass2 obj = new MyClass2(); obj.SomethingHappened += somethingHappened; obj.Method1(); } private static void somethingHappened(object sender, EventArgs e) { Console.WriteLine("Something happened!"); } } public class MyClass2 { public void Method1() { MyClass3 obj = new MyClass3(); obj.SomethingHappened += onSomethingHappened; obj.Method1(); } public event EventHandler SomethingHappened; private void onSomethingHappened(object sender, EventArgs e) { var handler = SomethingHappened; if (handler != null) { handler(this, e); } } } public class MyClass3 { public void Method1() { onSomethingHappened(); } private void onSomethingHappened() { var handler = SomethingHappened; if (handler != null) { handler(this, new EventArgs()); } } public event EventHandler SomethingHappened; } } 

One thing you might want to consider is what you do with the sender argument in the mediation class. You can do this with MyClass2 (as in the code above), or you can save the original sender as follows:

 private void onSomethingHappened(object sender, EventArgs e) { var handler = SomethingHappened; if (handler != null) { handler(sender, e); } } 
+1
source

If you want to avoid the callback solution and the event chain in each class, you have basically 2 solutions.

The first is to turn local variables of type MyClassX into fields, i.e. something like Chris Gessler, but fully following this approach and removing local vars.

 public static void main() { MyClass1 obj = new MyClass1(); obj.c2.c3.SomeEvent += obj_SomeEvent; obj.Method1(); } private static void obj_SomeEvent(object sender, EventArgs e) { Console.WriteLine("Some event fired"); } public class MyClass1() { public MyClass2 c2 = new MyClass2(); public void Method1() { c2.Method1(); } } public class MyClass2() { public MyClass3 c3 = new MyClass3(); public void Method1() { c3.Method1(); } } public class MyClass3() { public event EventHandler SomeEvent; private void OnSomeEvent() { if (SomeEvent!= null) { SomeEvent(this, new EventArgs()); } } public void Method1() { OnSomeEvent(); } } 

Another option (but it really depends on what you are trying to do, if possible, and I still don't like it) is to simply define the event in MyClass3 as static:

 public static void main() { MyClass3.SomeEvent += obj_SomeEvent; MyClass1 obj = new MyClass1(); obj.Method1(); } private static void obj_SomeEvent(object sender, EventArgs e) { Console.WriteLine("Some event fired"); } public class MyClass1() { public void Method1() { MyClass2 obj = new MyClass2(); obj.Method1(); } } public class MyClass2() { public void Method1() { MyClass3 obj = new MyClass3(); obj.Method1(); } } public class MyClass3() { public static event EventHandler SomeEvent; private void OnSomeEvent(MyClass3 anObj) { if (SomeEvent!= null) { SomeEvent(anObj, new EventArgs()); } } public void Method1() { OnSomeEvent(this); } } 
+1
source

All Articles