tags:

views:

197

answers:

1

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.

+1  A: 

You don't have a lot of great options. You can't use generics, that leaves you with the olden ArrayList class. Or an array. The COM interop layer will automatically generate a COM enumerator for a C# class that implements IEnumerable, 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.

Hans Passant
That sounds interesting, digging a little deeper I need to explicitly implement IEnumerable AND add a GetEnumerator method with the necessary attributes. I think if I can bodge together some sort of wrapper class this might just fly, thanks!
Andrew
@Andrew Do you really have to manually implement IEnumerable and add GetEnumerator? Can't you just use a `List`, which implicitly has them? I'd have thought the COM interop layer would be fine with that.
MarkJ