Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / October 2005

Tip: Looking for answers? Try searching our database.

Repost: Marshal array of Enums from VB6 to .NET

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Joe - 07 Oct 2005 21:03 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.