As I have been working in C # a long time as a tool where you can quickly make tools (ironically) with a friendly interface - quickly and conveniently too, at some point I had to add to a new class its own, so-called, Event (trigger, action, or whatever it is called). How to do it was obviously written in the MSDN Library,but you had to know exactly what you’re looking for to get there.
So, let see what do we have in an Event structure.
Well ok - let’s look at the structure of our Event.
public event EventHandler DoSome;
As you know, the word “public” gives access to the field to the outside of class, and ”DoSome”is the name of the field. ”Event” declares, of course, that we are dealing with the event. The most interesting is the ”EventHandler”. This is called a “delegate”, creation of specialized programming functionality to store adresses of methods from this or another class - you can see here the lost functionality of C + + to store the adresses of class methods in pointers. The lone keyword “delegate” has a bit more functionality (such as, for example, creating methods without names), but we need it now for something else. “EventHadnler” is an already built-in delegate, who has already an organized structure – its declaration should look something like this (as indeed it was already written in MSDN):
public delegate void EventHandler( object sender, EventArgs e );
As you can see the structure resembles the function declaration.The first argument is probably obvious, while the latter represents the class that was created to use in events:
public class EventArgs{ public static readonly EventArgs Empty; public EventArgs(); }
As you can see the class is simplistic - it is fundamental to creatingthe so-called “event arguments”. As I want to create an event with its own arguments (and not an empty filler), so I have to write some code:
public class SomeEventArgs : public EventArgs { public string Some { get; set; } public SomeEventArgs( string some ){ this.Some = some; } }
Ok, so now I want to use the created element. Therefore, I am forced to create a new delegate for such a task, which would accept mynew class:
[public delegate void SomeEventHandler( object sender, SomeEventArgs se );
A little note here – we do not need to use the EventArgs class to create a delegate -we can totally dump it. The argument structure can be anything - what we write now is simply determined by the programming convention, so that everything can have arms and legs - although I saw sometimes that EventArgs was replaced with a simple string class.
Ok - we have already an EventHandler - now we have to use it.
public event SomeEventHandler DoSome;
To tell the truth, this is a full, ready event - it will be visible on the appropriate tab in the properties of the object in VisualStudio. On the other hand creating an event is pointless if we don’t invoke it somewhere, like for example, in some method:
DoSome.Invoke( this, new SomeEventArgs( "I did something!" ) );
A further support for an event takes place as usual. And, as the last question - watch out that the event shouldn’t be empty – if it is, call to the Invoke method will end up in an error message. The easiest way to fix this, is by loading inside an empty dummy method.
void OnDoSome( sender object, SomeEventArgs ce ) { } ... //somewhere int the init code DoSome += new SomeEventHandler( OnDoSome );
Of course, nothing stands in the way that the dummy code will actually do something. Just remember the operator ”+= ” is doing – all methods that ware assigned by it will be triggered(all of them!) when the event will be invoked. If we want to get rid of some of them - we can use the operator ”-=”.