CJ7
CJ7

Reputation: 23275

Use of VB.NET class in VB6 via interop requires class to have constructor?

When I try to create a VB.NET object via interop in VB6, I have noticed I get this error if my VB.NET class doesn't have a constructor:

Error 430 - Class doesn't support automation

All I have to do is put an empty constructor in the VB.NET class, eg:

Public Sub New()

End Sub

and the error is avoided. Is this the expected behaviour?

Upvotes: 2

Views: 967

Answers (1)

Hans Passant
Hans Passant

Reputation: 941257

VB6 creates objects through COM, using the class factory for a COM coclass. The underlying method is IClassFactory::CreateInstance(). This method does not permit passing any arguments to the factory. It therefore follows that the [ComVisible] .NET class must have a constructor that doesn't take any arguments.

.NET already creates a default constructor for a class, unless you specify a constructor yourself that takes arguments. Which will never be used, you might as well remove it. Now you also don't need the empty default constructor anymore.

Upvotes: 2

Related Questions