views:

171

answers:

4

hi, I'm having some real confusion about events in c#... if I have this code in an interface:

Event OnBeforeSaving(ByVal Sender As TEntity, ByVal EventArgs As CancelEventArgs)

How should it be in c#? When I run it through a converter it gives me this

event OnBeforeSavingEventHandler OnBeforeSaving;
delegate void OnBeforeSavingEventHandler(TEntity Sender, CancelEventArgs EventArgs);

I'm not sure if I understand what is going on... in my head the code should be combined. Is this correct?

event OnBeforeSaving(TEntity Sender, CancelEventArgs EventArgs);
+1  A: 

.Net requires events to be of a Delegate type.

The VB compiler will automatically create a delegate type; the C# compiler forces you to create it yourself.

SLaks
+9  A: 

I'm not sure if I understand what is going on... in my head the code should be combined. Is this correct?

No. In VB.NET, you can combine this on a single line. The Event keyword allows you to specify the full signature of the delegate type being handled.

In C#, however, you need to explicitly tell the event which type of delegate it will use. If it's not a standard delegate type, then you have to declare the delegate, as well. This is what your converter is doing for you.

That being said, in this case, this:

delegate void OnBeforeSavingEventHandler(TEntity Sender, CancelEventArgs EventArgs);
event OnBeforeSavingEventHandler OnBeforeSaving;

Probably should be replaced with this:

event EventHandler<CancelEventArgs> OnBeforeSaving;

This is because there is a built-in EventHandler<T> type in the framework, that follows the suggested pattern for events, which specifies that the sender should be an System.Object, and the EventArgs should be a subclass of EventArgs. This is not quite the same as your VB.NET code, however, since you were restricting the sender to a TEntity type.

Even better would be to use the built-in CancelEventHandler type:

event CancelEventHandler OnBeforeSaving;

This is basically identical to EventHandler<CancelEventArgs>, but more expected, since there is a framework event handler type specifically for cancellation.

Reed Copsey
`CancelEventArgs` does have a specific matching delegate type. Users familiar with this event will likely put `+= new CancelEventHandler...` based on prior experience: http://msdn.microsoft.com/en-us/library/system.componentmodel.canceleventhandler.aspx
280Z28
True, good point... In this case, though, it wouldn't have worked before either ;) but here, using CancelEventHandler would make the most sense.
Reed Copsey
Edited to add that - thanks 280Z28!
Reed Copsey
Not to nitpick (ok maybe to nitpick, but I already voted it up), but the event should be called `BeforeSaving`, and there should be a `protected virtual void OnBeforeSaving(CancelEventArgs e)` method that invokes it.
280Z28
@280Z28: Your first comment (about the event name) should be made to the original poster, who named it in VB as OnBeforeSaving.
R. Bemrose
@280Z28: +1, and yes, I agree completely - but again, I was trying to follow the OP's naming...
Reed Copsey
Yeah I know. :) Surprised you didn't call me back on the name - I believe either `BeforeSave` or simply `Saving` are the best choices?
280Z28
Thanks very much. Great answer!
@280Z28: I personally would use Saving and Saved, and provide both events (to correspond to closing/closed, etc).
Reed Copsey
A: 
event EventHandler<CancelEventArgs> OnBeforeSaving;
Jamie Ide
A: 

This line:

delegate void OnBeforeSavingEventHandler(TEntity Sender, CancelEventArgs EventArgs);

defines a new type called "OnBeforeSavingEventHandler". It is a delegate type, which defines a method call that takes TEntity and CancelEventArgs parameters and returns nothing.

This line:

event OnBeforeSavingEventHandler OnBeforeSaving; 

declares a class member that is an event called "OnBeforeSaving". This event is of type "OnBeforeSavingEventHandler". Therefore, any objects that wish to subscribe to this event must have a method that is compatible with the "OnBeforeSavingEventHandler" delegate type.

Jeffrey L Whitledge