Casting returned object

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Stuart Miles (spamtrap_at_moleshome.co.uk)
Date: 03/03/05


Date: Thu, 3 Mar 2005 15:32:46 +0000

I have a 3rd party DLL with a function that has a return parameter of type
object.

After I make the call:

  messagingClass.eGetUnreadMessages(ref myId, ref msgs);

when I look at it in the Locals window I get the following:

- msgs {System.Array} System.Object
        [0,0] "AFPSDEV1" string
        [0,1] "AFPSDEV1" string
        [0,2] "AFPSDEV1" string
+ [1,0] {12002} System.Int32
+ [1,1] {12003} System.Int32
+ [1,2] {12004} System.Int32

This looks like an array of eMsgIdentType so I try and cast it to that

  eMsgIdentType[] unreadMsgs = (eMsgIdentType[])msgs;

But I get the following error:

An unhandled exception of type 'System.InvalidCastException' occurred in
Serialization.exe

Additional information: Specified cast is not valid.

As a complete newbie to C# can somebody point me in the right direction.