简体   繁体   中英

Implementing an interface with more general methods

I have this interface:

Public Interface IDocumentSavingEventArgs
    Inherits IDocumentCancelEventArgs

    Property SuggestedDocName As String
    Property SuppressSaveDialog As Boolean
End Interface

which, as shown, inherits from a more general interface IDocumentCancelEventArgs .

Then I have this interface:

Public Interface IDocumentSavingHandlerProvider
    Inherits IProvider

    Sub DocumentSavingHandler(sender As Object, e As IDocumentSavingEventArgs)
End Interface

For old pulgins compatibility purposes, I need to implement the latter interface also using an e of type IDocumentCancelEventArgs :

Public Sub MySavingHandler(sender As Object, e As IDocumentCancelEventArgs)
    Implements IDocumentSavingHandlerProvider.DocumentSavingHandler

This seems not possible, as the compiler warns me that there is no DocumentSavingHandler method with that signature. At runtime, this should not be an issue, in my opinion, as MySavingHandler would accept an IDocumentSavingEventArgs for sure, since it's typeof IDocumentCancelEventArgs .

Is there a way to achieve this?

The compiler is correct, since IDocumentCancelEventArgs doesn't inherit from IDocumentSavingEventArgs it can't be cast to IDocumentSavingEventArgs. Then you can test in your implementation if e is IDocumentCancelEventArgs or IDocumentSavingEventArgs.

You need to use the least common denominator IDocumentCancelEventArgs

Public Interface IDocumentSavingHandlerProvider
    Inherits IProvider

    Sub DocumentSavingHandler(sender As Object, e As IDocumentCancelEventArgs)
End Interface


Public Sub DocumentSavingHandler(sender As Object, e As IDocumentCancelEventArgs) Implements IDocumentSavingHandlerProvider.DocumentSavingHandler

    Dim saveEventArgs As IDocumentSavingEventArgs

    If TypeOf e Is IDocumentSavingEventArgs Then
        saveEventArgs = DirectCast(e, IDocumentSavingEventArgs)
    Else
        ' Do something else....
    End If


End Sub

As stated before, the compiler is correct. You need to have function signatures which match. I think what may be confusing here is even though you could pass a IDocumentSavingEventArgs to a function accepting IDocumentCancelEventArgs the compiler sees these definitions as two separate functions. If you want something more general, you may have to abstract those interfaces to another interface, which I wouldn't recommend since that gets un-maintainable fairly quickly, or you could create an overloaded function in your interface.

Public Interface IDocumentSavingHandlerProvider
    Inherits IProvider

    Sub DocumentSavingHandler(sender As Object, e As IDocumentSavingEventArgs)
    Sub DocumentSavingHandler(sender As Object, e As IDocumentCancelEventArgs)
End Interface

In the latter function you can convert the object to whatever you need it to be and then pass it to your main DocumentSavingHandler method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM