Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

When I started using Java, I created a custom Event class to mimic C#\'s impleme

ID: 645801 • Letter: W

Question

When I started using Java, I created a custom Event class to mimic C#'s implementation of Events. Since then, I've added some other features, like event listener priorities, and cascading events were priorities are respected. Now that I'm using C# again, I wanted to create an eventhandler class, or at least a class that I can use the event keyword with, that incorporated these features. Starting off with, I've hit a bit of a brick wall where I can't subclass EventHandler, being told that it's a sealed class, and I can only use the event keyword with EventHandler/EventHandler and its subclasses.

Is this a fool's errand? And should I just lump for a more straight-forward translation of my event class, ignoring the event system already in-place? Or is there I way I can do this successfully?

Explanation / Answer

I'm not sure I understood your question, but with the introduction of generics you don't need anymore to write a custom eventhandler, but you can create a custom EventArgs...

public event EventHandler<MyEventArgs> MyEvent;
public class MyEventArgs : EventArgs { //your stuff here }
if you really need a custom EventHandler you can write:

public delegate void CustomEventHandler(object sender, MyEventArgs e);
public event CustomEventHandler MyEvent

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote