Reputation: 2360
I have a problem when trying to raise an event of an object where the type implements an interface (where the event is) form another class.
Here is my code :
The interface IBaseForm
public interface IBaseForm
{
event EventHandler AfterValidation;
}
The Form that implement the interface IBaseForm
public partial class ContactForm : IBaseForm
{
//Code ...
public event EventHandler AfterValidation;
}
My controller : Here where the error occurs
Error message :
The event AfterValidation can only appear on the left hand side of += or -=(except when used from whithin the type IBaseForm)
public class MyController
{
public IBaseForm CurrentForm { get; set; }
protected void Validation()
{
//Code ....
//Here where the error occurs
if(CurrentForm.AfterValidation != null)
CurrentForm.AfterValidation(this, new EventArgs());
}
}
Thanks in advance
Upvotes: 3
Views: 3556
Reputation: 81179
An event is essentially a structure in .NET that identifies two methods -- an add
method and a remove
method; each of which accepts a delegate as a parameter.
Although events are often used with methods that combine passed-in delegates to, or remove passed-in delegates from, fields of type MulticastDelegate
, that is an implementation detail. While the normal purpose of creating an event is to have other code pass in delegates that they would like to have invoked under certain circumstances, the event mechanism in .NET doesn't care what, if anything, is done with the passed in delegates.
In some cases, it may be perfectly legal and logical for the add
and the remove
methods associated with an event to simply discard the passed-in delegates. For example, an immutable collection type might implement IObservableCollection
(so as to facilitate use with e.g. a control which is supposed to automatically update as needed to show the current collection state), but discard any delegates passed to update-notification events. If the collection is never going to be updated, it would never need to use a list of subscribers, and thus it would have no reason to keep one.
Because there is no requirement that an event must do anything with passed-in delegates, it's not possible for .NET events themselves to provide any functionality beyond exposing the add
and the remove
methods.
Although the descriptor for the event type in .NET includes a property which can specify a raise method in addition to add and remove, that feature is never used in practice. Preliminary designs for .NET might have intended that it be used, and removing any member of a type can cause compatibility issues in some corner cases such as deserialization, but outside of such issues the property may as well not exist.
Upvotes: 1
Reputation: 139768
You cannot raise an event from outside of the defining class.
You need to create a Raise()
method on your interface:
public interface IBaseForm
{
event EventHandler AfterValidation;
void RaiseAfterValidation();
}
public class ContactForm : IBaseForm
{
public event EventHandler AfterValidation;
public void RaiseAfterValidation()
{
if (AfterValidation != null)
AfterValidation(this, new EventArgs());
}
}
And in your controller:
protected void Validation()
{
CurrentForm.RaiseAfterValidation();
}
But it is usually a design smell if you want to trigger an event from outside the defining class... usually there should be some logic in the IBaseForm
implementations which triggers the event.
Upvotes: 4