Search code examples
vb.netcomvb6com-interopvb6-migration

Can I make a function COM Only? (Opposite of <Runtime.InteropServices.ComVisible(False)> )


I have some functions in my VB.NET DLL which I can 'hide' from my VB6 app by using the following:

<Runtime.InteropServices.ComVisible(False)> _

But is there a way to make a function ONLY visible to COM clients and not to .NET assemblies?

This way I can use Shared methods for the .NET side avoiding the need for an instance declaration.


Solution

  • You might be able to achieve what you want by using Explicit Interface Implementation. You can declare an interface that will be used by COM clients and another one for .NET clients, leaving all methods private in your implementation class. The code may look like this:

    Imports System.Runtime.InteropServices
    
        Public Interface ITestInterface
    
        <ComVisible(True)> _
            Sub MyTestMethod()    
        End Interface
    
        <ComVisible(True)> _
        Public Class TestClass
            Implements ITestInterface
    
            Private Sub MyTestMethod() Implements ITestInterface.MyTestMethod
            End Sub
        End Class
    


    I have to say that I do not understand what you mean with: "This way I can use Shared methods for the .NET side avoiding the need for an instance declaration. "