Andrew
Andrew

Reputation: 885

Marshal a list of objects from VB6 to C#

I have a development which requires the passing of objects between a VB6 application and a C# class library. The objects are defined in the C# class library and are used as parameters for methods exposed by other classes in the same library. The objects all contain simple string/numeric properties and so marshaling has been relatively painless.

We now have a requirement to pass an object which contains a list of other objects. If I was coding this in VB6 I might have a class containing a collection as a member variable. In C# I might have a class with a List member variable.

Is it possible to construct a C# class in such a way that the VB6 application could populate this inner list and marshal it successfully? I don't have a lot of experience here but I would guess Id have to use an array of Object types.

Upvotes: 3

Views: 1468

Answers (1)

Hans Passant
Hans Passant

Reputation: 941387

Options are more limited through COM than what you have in C#:

  • You can't use generics (COM does not have support for this, and TLBEXP will leave them out)

  • There is the olden ArrayList class. Or an array.

  • The COM interop layer will automatically generate a COM enumerator for a C# class that implements IEnumerable (the non-generic version), you can iterate it on the VB6 side with For Each.

  • Similarly, it generates an IEnumerable for a COM class that implements an COM enumerator. You can use foreach in C# code to enumerate a VB6 Collection. Choose between them depending on who creates the collection.

Upvotes: 2

Related Questions