Reposting in the hope that an MVP or someone from Microsoft can answer this.
I have an enum defined in managed code, e.g.:
public enum MyEnum
{
MyValue1,
MyValue2,
MyValue3
}
I have exposed a method which takes an array of MyEnum as an argument, e.g.:
public void MyMethod([In] ref MyEnum[] values)
{
...
}
And now I attempt to call this method from VB6 code as follows:
Dim myValues(0 To 2) As MyEnum
myValues(0) = MyEnum_MyValue1
myValues(1) = MyEnum_MyValue2
myValues(2) = MyEnum_MyValue3
Set obj = ... ' Instantiate .NET class
obj.MyMethod myValues
When I do this, I get an error at the line "obj.MyMethod myValues" as follows:
-2146233037 (80131533)
Specified array was not of the expected type
Why is this and is there a way I can marshal an array of Enums?
The generated typelib looks like I would expect it to, i.e. the argument is
[in] SAFEARRAY(MyEnum)* values
Robert Jordan - 07 Oct 2005 21:14 GMT
Hi,
> I have an enum defined in managed code, e.g.:
> public enum MyEnum
[quoted text clipped - 24 lines]
>
> Why is this and is there a way I can marshal an array of Enums?
I don't know the exact reason. AFAIK it's the same one why you
cannot pass the array by value. You may try this as a workaround:
public void MyMethodTest([In] ref object[] values)
{
MyEnum[] a = new MyEnum[values.Length]
for (int i = 0; i < values.Length; i++)
a[i] = (MyEnum) values[i];
}
Rob
Joe - 08 Oct 2005 11:51 GMT
Rob,
Thanks for the response.
I had thought of your workaround, but in my case the main reason for using
Enums is so that users of my class library will have Intellisense. This
won't work if I just pass an object array.
Is there really no way to do this? It does seem that COM Interop is very
restrictive.
> Hi,
>
[quoted text clipped - 38 lines]
>
> Rob