Search code examples
.netvb6com-interop

Can I have two classes implement the same interface in COM?


My classes are as follows

[ComVisible(True)][GUID(...)]
public interface IMyComInterface
{
   void DoThis(string[] params);
}

[ClassInterface(ClassInterfaceType.None)]
[GUID(...)]
public class MyComImplementation: MyNetClass,IMyComInterface
{
   public void DoThis(string[] params)
   {
      var netParams=params.ToList();
      base.DoThis(netParams);
   }
}

This works.

Now I want to add a second ComClass that inherits MyNetBaseClass but uses a filter:

[ClassInterface(ClassInterface.Type.None)
[GUID(...)]
public class MySecondComImplementation:MyNetClass,IMyComInterface
{
   public void DoThis(string[] params)
   {
      var netParams=params.Where(param=>param.Contains("x"));
      base.DoThis(netParams.ToList());
   }
}

Does this work, or do I have to define a second MyComInterface with an identical DoThis method? Or in other words: must every class that implements a COM-Visible interface have its own, unique COM-visible interface?


Solution

  • The example you show, is perfectly legal. The 2 classes will each have their own GUID. By registering the assembly, the necessary entries will be written in the registry.

    In your VB application, you'll be able to do this. (Sorry, there can be syntax errors in the code below, I don't have enough VB6 knowledge:) ).

    Dim a As IMyComInterface
    Dim b As IMyComInterface
    
    Set a = New MyComImplementation()
    Set b = New MySecondComImplementation()