This question is a follow-up to my last question with a reference to COM Interop.
Let’s say I have the following 2 interfaces and implementing classes:
public interface ISkuItem
{
public string SKU { get; set; }
}
public interface ICartItem : ISkuItem
{
public int Quantity { get; set; }
public bool IsDiscountable { get; set; }
}
public class CartItem : ICartItem
{
//implemented properties...
}
Or, in VB.NET:
Public Interface ISkuItem
Property SKU() As String
End Interface
Public Interface ICartItem
Inherits ISkuItem
Property Quantity() As Integer
Property IsDiscountable() As Boolean
End Interface
Public Class CartItem
Implements ICartItem
'Implemented Properties'
End Class
Interfaces are important in COM interop for exposing properties and methods to IntelliSense in the VB6 IDE (per this article). However, because ICartItem is inherited from ISkuItem, SKU is not explicitly defined in ICartItem and thus is not visible in IntelliSense in VB6 and even throws a compiler error when trying to write to objCartItem.SKU.
I’ve tried using Shadows and Overloads on the SKU property in ISkuItem, but then the compiler wants me to explicitly implement SKU for both ISkuItem and ICartItem within the CartItem class. I don’t think that’s what I want.
Is there a way (in either VB.NET or C#) to explicitly declare the SKU property in ICartItem without having to declare SKU twice in the CartItem class?
You could do as Matthew suggests and provide a new (
Shadows)SKUmember toICartItem. But as I’m sure you realize, this pretty much renders your interface inheritance useless.It really seems like this interface inheritance model isn’t working out for you.
Are you sure that having
ICartIteminherit fromISkuItemis actually buying you anything?Rather than suffer a headache over it, I’d probably be inclined to just have
ICartItemoffer its ownSKUproperty and not inherit fromISkuItemat all. Then any class implementingICartItemcould optionally also implementISkuItemexplicitly (just as many collection classes in .NET do, e.g. withICollection<T>andICollection); but either way it will have anSKUproperty.This isn’t the design choice I would make in general, mind you (I would go with what you have); but when it comes to COM interop, sometimes you just have to make sacrifices because you don’t want to bang your head against the wall forever.
I’m not sure you’re going to get a very satisfying answer on this one. VB6 just flat-out doesn’t understand everything that .NET does, so you’re not going to be able to figure out a perfect 1:1 mapping of .NET features to COM equivalents.