Conflict between event in base class and implementation of interface event in derived class

て烟熏妆下的殇ゞ 提交于 2019-12-24 15:53:20

问题


Base class:

Public MustInherit Class Connector

    Public Event Changed as EventHandler

End Class

Interface:

Public Interface IPlug

    Event Changed as EventHandler

End Interface

Derived class:

Public Class OutputConnector
    Inherits Connector
    Implements IPlug

    Event Changed as EventHandler Implements IPlug.Changed

End Class

VB.Net Problem:

The Changed event in OutputConnector conflicts with the Changed event in Connector, of course. But I have to implement it to satisfy the IPlug interface.

How can I solve this?

I have other classes deriving from Connector (e.g. an InputConnector class) that need to have the Changed event, but NOT implement IPlug. And I don't want derived classes that are implementing IPlug to have an additional event just to avoid the name conflict (which would be the case if I changed the name of the event in either the base class or the interface).

Any suggestions?

Edit:

The problem is not the names per se, but that I want the derived class to somehow map its interface event implementation to the existing event in the base class - and not have to declare an additional event in the derived class (with all sorts of routing clutter to boot); after all, the event already exists in the base class! What I need is a way to let the interface know that..

A pseudo-solution (just to illustrate) would be to be able to put something to the effect of

Public Event MyBase.Changed Implements IPlug.Changed

in the derived OutputConnector class.


回答1:


This question points out two ways to make an interface implementation explicit.

  • Make it private
  • Use the fact that the name of the implementation need not match the interface name.

So it depends on how you plan on using IPlug.Changed. If you will always be using the interface to interact, then you can simply make it private, and even if it requires Shadows it won't be visible via the public API.

The other way is to decide a new name for the declaration, such as OutputConnector.IPlugChanged.

Which you select depends on how you are using everything.

EDIT:

You seem to want the two events to be identical in functionality.

In that case the best thing to do would be to hide the IPlug.Changed implementation by making it private with a different name.

Once you have hidden IPlug.Changed, use your OnChange protected method (assuming you made one) to link up the two, calling IPlugChanged when OnChange is triggered.

If you don't have an OnChange protected method, you can attach an event that performs the above call instead. Note that if you are worried about the order events are called in, this won't work.

A final option may be to use the Custom syntax to stream changes to IPlug to the base class.



来源:https://stackoverflow.com/questions/14825021/conflict-between-event-in-base-class-and-implementation-of-interface-event-in-de

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!