tags:

views:

114

answers:

6

In my class I want to declare an event that other classes can subscribe to. What is the correct way to declare the event?

This doesn't work:

public event CollectMapsReportingComplete;
+5  A: 

You forgot to mention the type. For really simple events, EventHandler might be enough:

public event EventHandler CollectMapsReportingComplete;

Sometimes you will want to declare your own delegate type to be used for your events, allowing you to use a custom type for the EventArgs parameter (see Adam Robinson's comment):

public delegate void CollectEventHandler(object source, MapEventArgs args);

public class MapEventArgs : EventArgs
{
    public IEnumerable<Map> Maps { get; set; }
}

You can also use the generic EventHandler type instead of declaring your own types:

public event EventHandler<MapEventArgs> CollectMapsReportingComplete;
Jørn Schou-Rode
Not going to downvote for this, but the convention in .NET is that event delegates should only include an (`object`) sender and one additional parameter that inherits from `EventArgs`. Doing this will also allow you to use the generic `EventHandler<TEventArgs>` delegate type rather than having to declare you own. There is, of course, nothing wrong with your approach (it'll work just fine), but the prevailing wisdom favors the EventArgs-based approach.
Adam Robinson
@Adam: You are absolutely right, and I have updated my answer to reflect this.
Jørn Schou-Rode
+1  A: 

You need to specify the delegate type the event:

public event Action CollectMapsReportingComplete;

Here I have used System.Action but you can use any delegate type you wish (even a custom delegate). An instance of the delegate type you specify will be used as the backing field for the event.

Andrew Hare
Tilendor
`EventHandler` and `Action` are both delegate types - you can freely specify any delegate type you wish.
Andrew Hare
using `Action` doesn't follow the .NET standard event syntax...
thecoop
The OP didn't ask about .NET's event _pattern_ they asked for the correct _syntax_ for declaring an event. It is important to understand that events can be declared with any delegate type.
Andrew Hare
@Andrew: One could argue it's just as important that they understand the prevailing pattern so that they don't unnecessarily go down the road of arbitrary event delegates only to discover that they don't fit nicely with the accepted pattern.
Adam Robinson
@Adam Robinson - Good point! I agree in part but I also feel that prevailing patterns can often restrict understanding of language concepts. It is good to point out though. :)
Andrew Hare
@Andrew: He (and everyone) should definitely know the syntax and the mechanics, which you showed. I just think it's also good for them to know "this is how most people do it". :)
Adam Robinson
@Adam - Fair enough! :)
Andrew Hare
+1  A: 
public event EventHandler MyEvent;
Josh
+2  A: 

The other answers are good. However, there's a good tutorial here if you need more in-depth information.

David Stratton
A: 

An Example

/// </summary>
/// Event triggered when a search is entered in any <see cref="SearchPanel"/>
/// </summary>
public event EventHandler<string> SearchEntered
{
    add { searchevent += value; }
    remove { searchevent -= value; }
}
private event EventHandler<string> searchevent;
galford13x
A: 

public event [DelegateType] [EventName];

tsinik