views:

316

answers:

2

Hello, experts.

I work in VB.NET v2

I have an interface IMyInterface and this interface implements a method MyMethod.

I have an object MyObjectBase. This object contains a(the same) method MyMethod.

1) If now I do MyObject Inherits MyObjectBase Implements IMyInterface need I to redefine? (shadow, override) MyMethod in the MyObject class?

2) What now if instead the MyMethod method I have an MyEvent event?

Thanks.

A: 

It's because you implement your child class that you have to redefine it. I believe if you just inherit from the base case, which is implemented anyway, you won't have to if you don't need it.

David Brunelle
I don't understand... In C# I am not forced to do it. An interface garantees that a method is implemented. Now, if I already have this method implemented in the base class, what is the compiler problem?
serhio
... and if you do need to modify the implementation of MyMethod, you could have a protected overrideable method in the base class, so that you can modify the implementation without needing to reimplement the interface. Probably a cleaner way?
MarkJ
@serhio: the compiler problem is that VB only uses explicit interface implementation - i.e. you have to write Implements MyMethod. The C# compiler automatically matches a method to an interface elements if the method has the same name. It's one of the differences between VB and C#.
MarkJ
Well... I don't need to modify any methods. I like to keep this method as is. In my concrete case I have a *MyButton* inherits Button and a interface **IVisibleChanged** with a event **VisibleChanged**. the Button class have already the VisibleChanged event and I dont know what to do now.
serhio
If it were a sub you could just implement it with a different name: `Private Sub WhateverNameYouLike Implements IVisibleChanged.VisibleChanged` I don't know about events though. Anyone else?
MarkJ
It's quite similar for events: `Private Event MyVisibleChanged() Implements IVisibleChanged.VisibleChanged`. For details, see my answer. :-)
Heinzi
I think you still miss his point, guys. He inherited a member named `Foo` from a base class of his derived class. He wants to use _that inherited member_ as an implementation of similarly-named member `Foo` of an interface implemented by his derived class.
Pavel Minaev
@Pavel: I do understand that. And the answer is: That's not possible. You have to use a workaround, and the easiest workaround is to create a *new* member `TempFoo`, associate that member with `Foo` (interface), and have that new member invoke `Foo` (base class).
Heinzi
+5  A: 

In VB.NET, you need to manually associate your interface contract with your implementations. Have a look at the following example:

Interface MyInterface
    Sub Foo()
End Interface

Class TestClass
    Implements MyInterface

    Public Sub Test() Implements MyInterface.Foo
        Console.WriteLine("Test")
    End Sub

    Public Sub Foo()
        Console.WriteLine("Foo")
    End Sub
End Class

Then have a look at the following code and its output:

Dim x As New TestClass()
x.Foo()                           ' Output: Foo '
Dim y As MyInterface = x
y.Foo()                           ' Output: Test '

This has the advantage that implementing an interface does not restrict you in naming your function as you want to. The disadvantage is that you have to explicitly associate your class methods with your interface declarations using the Implements keyword.


So much about an explanation. Now let me get to your problem: Since you cannot make Button implement IVisibleChanged, you can do something like that:

Private Event MyVisibleChanged() Implements IVisibleChanged.VisibleChanged

Private Sub RethrowVisibleChanged() Handles MyBase.VisibleChanged
    RaiseEvent MyVisibleChanged()
End Sub

MyBase is a VB.NET keyword referring to the superclass. Likewise, in the case of MyMethod, you can do

Private Sub MyInterfaceMethod() Implements IMyInterface.Method
    MyBase.Method()
End Sub

This might seem like unnecessary extra work, but in a way, it makes sense: Button.VisibleChanged and IVisibleChanged.VisibleChanged might be events with two completely different semantics, who just happen to have the same name (after all Button does not implement IVisibleChanged). With your code, you explicitly create a connection between those two.

Heinzi
A further advantage is that the same function can implement multiple elements, possibly from different interfaces.
MarkJ
Thanks. Now, if I have an declared *Event* in a Interface and the same event in the base class. Can you give me an example of this? Thanks!
serhio
The example is in my answer, right below the separator line.
Heinzi
"you cannot make Button implement IVisibleChanged"... first of all, I can't agree it. Secondly, imagine the situation :__________________________________________________________________________________**AddHandler CType(myObject, IVisibleChanged).VisibleChanged, RunMe**; The RunMe function will never be called.
serhio