Implementing an interface through a form class using an event

John

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!

Patrick Hofman

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implementing interface using child class exception

From Dev

Anonymous class implementing an interface

From Dev

Base class implementing interface

From Dev

implementing a interface to a class: class to interface to interface

From Dev

Implementing an event inherited from an interface

From Dev

Implementing Java Interface with extra method in implementing Class

From Dev

Class design:class implementing an Interface implementing another interface

From Dev

Class design:class implementing an Interface implementing another interface

From Dev

Reflection - Class implementing an interface as parameter

From Dev

Struct vs class implementing an interface

From Dev

AutoConfiguredMoqCustomization with abstract class implementing interface

From Dev

Implement Serializable to a class implementing an Interface

From Dev

Implementing an Interface Parameter in Abstract Class

From Dev

Why Abstract Class Implementing Interface?

From Dev

Struct vs class implementing an interface

From Dev

Setting setter of an interface in class implementing it

From Dev

Triggering event from Class A to Class B using interface in Java

From Dev

PhpDoc for interface and class implementing interface - difference

From Dev

Class implementing interface containing same interface

From Dev

Implementing a interface and extending an class that implements the same interface?

From Dev

Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

From Dev

find type implementing generic interface through reflection

From Dev

Implementing a custom event handler from interface

From Dev

Creating and Implementing an interface using Python?

From Dev

Typescript interface declaration for form event

From Dev

Generic class with an interface constraint vs class implementing interface

From Dev

"Class does not implement interface member" error on class implementing a generic interface

From Java

Class implementing an interface is not accepted for generics wildcard

From Dev

Generics where T is class implementing interface

Related Related

  1. 1

    Implementing interface using child class exception

  2. 2

    Anonymous class implementing an interface

  3. 3

    Base class implementing interface

  4. 4

    implementing a interface to a class: class to interface to interface

  5. 5

    Implementing an event inherited from an interface

  6. 6

    Implementing Java Interface with extra method in implementing Class

  7. 7

    Class design:class implementing an Interface implementing another interface

  8. 8

    Class design:class implementing an Interface implementing another interface

  9. 9

    Reflection - Class implementing an interface as parameter

  10. 10

    Struct vs class implementing an interface

  11. 11

    AutoConfiguredMoqCustomization with abstract class implementing interface

  12. 12

    Implement Serializable to a class implementing an Interface

  13. 13

    Implementing an Interface Parameter in Abstract Class

  14. 14

    Why Abstract Class Implementing Interface?

  15. 15

    Struct vs class implementing an interface

  16. 16

    Setting setter of an interface in class implementing it

  17. 17

    Triggering event from Class A to Class B using interface in Java

  18. 18

    PhpDoc for interface and class implementing interface - difference

  19. 19

    Class implementing interface containing same interface

  20. 20

    Implementing a interface and extending an class that implements the same interface?

  21. 21

    Why would one use IEnumerable<T> in an interface definition if the class implementing the interface will be using List<T>?

  22. 22

    find type implementing generic interface through reflection

  23. 23

    Implementing a custom event handler from interface

  24. 24

    Creating and Implementing an interface using Python?

  25. 25

    Typescript interface declaration for form event

  26. 26

    Generic class with an interface constraint vs class implementing interface

  27. 27

    "Class does not implement interface member" error on class implementing a generic interface

  28. 28

    Class implementing an interface is not accepted for generics wildcard

  29. 29

    Generics where T is class implementing interface

HotTag

Archive