What is the proper way to pass an array of user defined classes from vba to .net (specifically c#) using com-interop?
Here's my c# code. If I call Method1 from vba it's failing with "Array or userdefined type expected" or "Function uses an automation type not supported in visual basic".
public class MyClass
{
public Method1(UserDefinedClass[] Parameters) { ... }
public Method2(Object Parameters) { ... }
}
I've read a bit about the MarshallAsAttribute class. Could this be the missing piece in the c# code?
Here's the vba code I'm using:
Dim udt As New UserDefinedClass
Dim myArray()
myArray(1) = udt
myClass.Method1(myArray)
myClass.Method2(myArray)
IIRC you have to pass arrays by reference.
Try declaring your method as
If you don't want to pollute your class with ref parameters for .NET clients, you can define a ComVisible interface to be used by COM clients, and implement it explicitly thus:
** In response to comment ** If you want to expose a collection instead of an array to VBA, you just need to expose an enumerator, and any other methods you want the VBA code to be able to call (e.g. Add, Remove, Insert, Clear, ...). E.g.
You can then use it as usual in VBA: