通过接口暴露事件时出错

时间:2010-05-19 20:29:10

标签: .net vb.net

我有这个界面

Interface IProDataSource

    Delegate Sub DstartingHandler(ByVal sender As Object, ByVal e As EventArgs)
    Event starting_Sinc As DstartingHandler

End Interface

尝试使用像这样的界面

Public Class DataSource : Implements IProDataSource

    Public Event starting_Sinc As DstartingHandler Implements IProDataSource.starting_Sinc
    Public Delegate Sub DstartingHandler(ByVal sender As Object, ByVal e As EventArgs)

End Class

给我下一个错误

  

事件'starting_Sinc'无法实现   接口上的事件'starting_Sinc'   'IProDataSource'因为他们的   委托类型'DstartingHandler'和   'IProDataSource.DstartingHandler'做   不匹配。

2 个答案:

答案 0 :(得分:1)

原因是您现在在应用程序中定义了2个委托DstartingHandler的实例。一个位于DataSource内,另一个位于IProDataSource内。 DataSource中的那个似乎是一个错误,删除它应该可以解决所有问题。

修改

我尝试了以下代码并编译

Class C1
    Implements IProDataSource

    Public Event starting_Sinc(ByVal sender As Object, ByVal e As System.EventArgs) Implements IProDataSource.starting_Sinc
End Class

答案 1 :(得分:1)

您需要在接口外部移动委托声明并将其声明为public。当实现它们的类是公共的时,接口使用的所有类型必须是公共的。必然如此或客户端代码永远不会分配事件。因此:

Public Delegate Sub DstartingHandler(ByVal sender As Object, ByVal e As EventArgs)

Interface IProDataSource
    Event starting_Sinc As DstartingHandler
End Interface

Public Class DataSource : Implements IProDataSource
    Public Event starting_Sinc(ByVal sender As Object, ByVal e As System.EventArgs) Implements IProDataSource.starting_Sinc
End Class

如果限制课程的可访问性,则可以使用原始方法:

Interface IProDataSource
    Delegate Sub DstartingHandler(ByVal sender As Object, ByVal e As EventArgs)
    Event starting_Sinc As DstartingHandler
End Interface

Friend Class DataSource : Implements IProDataSource
    Public Event starting_Sinc(ByVal sender As Object, ByVal e As System.EventArgs) Implements IProDataSource.starting_Sinc
End Class