3

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?

  • You should probably look up what exactly "delegate" means in C#. EventHandler is not really a class, it's a delegate. And you can use event with any delegate type. – svick Nov 21 '14 at 22:17
2

Just leave out the event keyword, instead of

public event SampleEventHandler SampleEvent;

use just standard delegates (in C#, delegates are multicast enabled). Declare it like this:

delegate void SampleEventHandler(object sender, MyEventArgs e);

Now, for introducing priorities, use something like a priority queue. As a simple form, you can utilize a dictionary:

Dictionary<int,SampleEventHandler> EventDictionary;

where the dictionary keys are the different priorities. This dictionary serves as an "event" in the class where you define it. To add some new event handler, you need something like this:

public void AddNewEvent(int priority,SampleEventHandler handler)
{
     if(!EventDictionary.ContainsKey(priority))
         EventDictionary.Add(priority, null );
     EventDictionary[priority]+=handler;
}

To raise the events in low-prio-first order, you need some code like this:

foreach(int priority in EventDictionary.Keys.OrderBy(p=>p))
     EventDictionary[priority](this,myEventArgs);
| improve this answer | |
1

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
| improve this answer | |
  • I want to write a custom EventHandler class that incorporates additional functionality, like priorities. – Hanii Puppy Nov 21 '14 at 16:24
  • Priorities of what? Of handlers execution? – Defkon1 Nov 21 '14 at 16:34
  • Handlers are actually executed in the order they are registered, but you can't rely that this implementation will not change in future. If you REALLY need a strong order in handlers execution you can check the samples you can find stackoverflow.com/questions/9622614/… and stackoverflow.com/questions/1645478/… – Defkon1 Nov 21 '14 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.