Reputation: 767
I have recently attempted to port a legacy VB6 application to .NET. It's not a complete port however as I'm focusing solely on the application layer. I'll still be consuming a handful of VB6 COM components.
So far so good, but I recently hit a wall.
I need to pass a custom collection from .NET to VB6. Is this possible?
The COM method I'm calling on the .NET side is expecting a generic object however the application blows up immediately with: InvalidCastException - Specified Cast Not Valid.
I've stumbled upon a few articles that hint on how to pass VB6 collections to .NET but not the other way around. I've tried inheriting from / utilizing a few different collection types in .NET but all to no avail so far. Any help is appreciated.
Upvotes: 1
Views: 787
Reputation: 761
In the COM interface declaration implemented by your .NET collection class, set the DispId for the method that returns your enumerator to -4. Call that method "GetEnumerator".
On the other hand, if you're actually wanting to mock a VBA.Collection object, and not just support For Each in VB6, you can set a reference to the VBA type library in your VB.NET class library (assuming you're creating a class library) and implement _Collection in your VB.NET class.
<Guid("A4C4671C-499F-101B-BB78-00AA00383CBB")>
<ComDefaultInterface(GetType(_Collection))>
<ComVisible(False)>
Public Class VBACollection
Implements _Collection
.
.
.
End Class
Then just implement the _Collection interface. If VB.NET doesn't like the underscore in the interface name, you may need to escape it.
Upvotes: 1