Jeff
Jeff

Reputation: 36583

VB.NET cross compatibility with C#

I have a C# project with a generic interface

public interface IMyFoo<T> { void DoSomething(T instance); }

I also have a C# project with an interface that inherits from several IMyFoos

public interface IMyBar : IMyFoo<Type1>, IMyFoo<Type2> { ... }

Everything works fine in C# land (including the scenario below which doesn't work in VB).

I have a VB .NET project that references this C# library.

I have an instance of IMyBar and try to use as follows:

Dim instance as MyType1 = ...
Dim bar as IMyBar = ...
bar.DoSomething(instance) ' This generates a compile error: 
' 'DoSomething' is ambiguous across the inherited interfaces 'IMyFoo(Of MyType1)' and 'IMyFoo(Of MyType2)'. 

What's up? I can DirectCast like this and it works fine...but I'd REALLY rather not

DirectCast(bar, IMyFoo(Of MyType1)).DoSomething(instance)

Upvotes: 4

Views: 710

Answers (2)

Chris Haas
Chris Haas

Reputation: 55457

Just to go into further details, the reason VB doesn't directly support what you are doing is because what you showed is just a special case of a class implementing two interfaces with the same method. Whenever VB sees this it forces a cast to make it explicit which one you are intending to use. The VB designers decided that this would make code less error-prone. C# goes further and assumes that you know what you are doing and lets you make that call. You can make C# get the same basic error by using the more generic case of two interfaces with the same method:

public interface IMyFoo1 { void DoSomething(string instance); }
public interface IMyFoo2 { void DoSomething(string instance); }
public interface IMyBar : IMyFoo1, IMyFoo2 { }
public class MyTestClass : IMyBar
{
    //Explicit interface declaration required
    void IMyFoo1.DoSomething(string instance) { }
    void IMyFoo2.DoSomething(string instance) { }
}

string s = "";
IMyBar bar = new MyTestClass();
bar.DoSomething(s);//The call is ambiguous between the following methods or properties...

Upvotes: 1

Ta01
Ta01

Reputation: 31630

You're probably going to have to cast:

Unlike other types, which only derive from a single base type, an interface may derive from multiple base interfaces. Because of this, an interface can inherit an identically named type member from different base interfaces. In such a case, the multiply-inherited name is not available in the derived interface, and referring to any of those type members through the derived interface causes a compile-time error, regardless of signatures or overloading. Instead, conflicting type members must be referenced through a base interface name.

Upvotes: 6

Related Questions