I’m implementing a project in MVP pattern and I’ve got some interfaces like this:
public interface IWizardStep { event EventHandler NextStep; } public interface ISubmitable { event EventHandler Submit; } public interface IStep : ISubmitable, IWizardStep { string SomeProperty { get; set; } }
My presenter class then has this:
public class StepPresenter { public IStep View { get; set; } public StepPresenter() { this.View.Submit += new EventHandler(StepSubmitted); } void StepSubmitted(object sender, EventArgs e){ //do some processing //Now I want to raise the Next event //this.View.NextStep(sender, e) throws a compile error } }
So I’m getting a compile error when trying to raise the NextStep event on the within the View. I’m pretty sure that it’s a problem because I haven’t implemented an event handler for NextStep, but this presenter wont be handling the event.
The presenter will be implemented as 1 step of many in a wizard on a form (ASP.NET, but that doesn’t make any difference) and the wizard will catch the NextStep event and show the next step in the wizard.
So how can I have an event on an interface raised like this, or is it impossible?
This is because NextStep is an eventHandler. You cannot call an event handler which must be raised. I would expect IStep to have a method call such as MoveToStep() which the concrete implementation would raise the event as expected which you could subscribe to somewhere else.
What is your reasoning for the NextStep eventHandler in IWizardStep? If its a methd you’re after, create that in your interface instead of an event. Your consumer class should expect to call a method when needing to ‘do’ something and subscribe to an event when the IWizardStep has something to pass back (data or whatever)