jump to navigation

COM Interop: int[*] cannot be cast to… anything useful? 12 June 2011

Posted by golda972 in C#, exception, interop, Windows.
trackback

What’s to be done with a non-zero based array in .NET?

Back in the day of VB6, arrays were commonly started at 1. So you had arrays of three members: 1, 2, and 3.

But VB6 is no longer being supported, and today we have (in its place?) VB.NET and, of course C#. Theoretically all arrays forever after shall be 0-based… except. VBA (which looks a lot like VB6) still uses 1-based arrays. And Microsoft Office’s Interop libraries are in VBA.

So a property like Range().SynonymInfo.PartOfSpeechList returns int[*], a 1-based array of integers. Which is utterly useless in .NET.

All the old posts (on StackOverflow, eg) suggest casting a non-zero based array (like int[*]) to the untyped Array. This works fine through .NET 3.5. In .NET 4.0 this cast is invalid, though.

[Update]

Cast to Object, and then you can cast to Array… even in .NET 4.0:

Object objThings = (Object)Range().SynonymInfo.PartOfSpeechList;

Array things = (Array)objThings;

Comments»

No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: