In both
VB.NET and C# you need to implement two functions/methods in the type implementing the interfaces with the duplicate method name, one funtion/method for each version of the duplicate method:
The C# compiler supports "interface-qualified names" for methods, but the
VB.NET compiler doesn't. So the
VB.NET version would need to look something like this:
Code:
Public Interface InterfaceA
Function ReturnAString() As String
End Interface
Public Interface InterfaceB
Function ReturnAString() As String
End Interface
Class Class1
Implements InterfaceA
Implements InterfaceB
Public Function InterfaceAReturnAString() As String _
Implements InterfaceA.ReturnAString
Return "InterfaceA implementation"
End Function
Public Function InterfaceBReturnAString() As String _
Implements InterfaceB.ReturnAString
Return "InterfaceB implementation"
End Function
End Class
Module Module1
Sub Main()
Dim TheClass As Class1 = New Class1
Dim TheString As String
TheString = TheClass.InterfaceAReturnAString()
TheString = TheClass.InterfaceBReturnAString()
End Sub
End Module
In C# you would use interface-qualified method names in the type implementing the interfaces, then cast the type to the interface whose version of the method you want to execute. So the C# version would need to look something like this:
Code:
using System;
public interface InterfaceA {
string ReturnAString();
}
public interface InterfaceB {
string ReturnAString();
}
class Class1 : InterfaceA, InterfaceB {
// Notice no access modifier. Method considered private.
// Can't be called by a variable referencing the type
// because the interface method is implemented using
// an "ïnterface-qualified name".
string InterfaceA.ReturnAString() {
return "InterfaceA implementation";
}
string InterfaceB.ReturnAString() {
return "InterfaceB implementation";
}
static void Main() {
Class1 TheClass = new Class1();
string TheString;
// Return InterfaceA's implementation. Cast the reference
// to TheClass to an InterfaceA, and only the method defined
// by interfaceA is callable.
TheString = ((InterfaceA)TheClass).ReturnAString();
// Return InterfaceB's implementation.
TheString = ((InterfaceB)TheClass).ReturnAString();
}
}
HTH,
Bob