I'm currently working on porting over some Visual Basic .NET code to c# .NET, and there's a particular part of the library I'm porting that I can't seem to get to work. Here's how it's laid out:
First, I have an interface defined as follows:
public interface IMyInterface
{
void EnableSave(object sender, EventArgs e);
}
Next, I have an inheritable form that implements the above interface:
public partial class frmIMyInterface : Form, Globals.IMyInterface
{
public delegate void EnableFormSaveEventHandler(object sender, EventArgs e);
public event EnableFormSaveEventHandler EnableFormSave;
public void EnableSave(object sender, EventArgs e)
{
EnableFormSave.Invoke(sender, e);
}
}
So far so good. Then, I create a form that implements frmIMyInterface:
public partial class frmMyNewForm : frmIMyInterface
{
...
In that form, I have a method defined to handle the event:
private void EnableSave(object sender, EventArgs e)
{
this.cmdSave.Enabled = true;
}
Then, in the designer code for the form, the handler delegate is defined as follows:
this.EnableFormSave += new frmIMyInterface.EnableFormSaveEventHandler(this.EnableSave);
My project builds normally, however, when I try to open frmMyNewForm
with the form designer, I get a splash screen that says, "To prevent possible data loss before loading the designer, the following errors must be resolved: The Method 'EnableSave' cannot be the method for an event because a class this class derives from already defines the method."
The VisualBasic.NET code that I ported had everything defined exactly as I have it defined, but worked fine. I can't see any problem, and it seems like .NET should know the difference between the method definition in the base class that raises the event, and the handler defined in the derived form that gets set as the event handler.
Thank you in advance!
You have to rename either your event handler or your event. Otherwise the call is ambiguous.
So this works:
this.EnableFormSave += new frmIBGInterface.EnableFormSaveEventHandler(this.OnEnableSave);
private void OnEnableSave(object sender, EventArgs e)
{
this.cmdSave.Enabled = true;
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments