By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,671 Members | 1,387 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,671 IT Pros & Developers. It's quick & easy.

Casting returned object

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Try IList instead. Then you should be able to use the standard someList[x]
to access elements

JIM

"Stuart Miles" <sp******@moleshome.co.uk> wrote in message
news:1k******************************@40tude.net.. .
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.

Nov 16 '05 #2

P: n/a
Try doing this:

MessageBox.Show(messagingClass.eGetUnreadMessages( ref myId, ref
msgs).GetType().FullName);

this should show you the type name of the class being returned. From there
you should be able to cast the result to the right type and get access to
the properties.
--
Thanks
Wayne Sepega
Jacksonville, Fl
"When a man sits with a pretty girl for an hour, it seems like a minute. But
let him sit on a hot stove for a minute and it's longer than any hour.
That's relativity." - Albert Einstein

"Stuart Miles" <sp******@moleshome.co.uk> wrote in message
news:1k******************************@40tude.net.. .
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.

Nov 16 '05 #3

P: n/a
On Thu, 3 Mar 2005 11:03:13 -0500, Wayne wrote:
Try doing this:

MessageBox.Show(messagingClass.eGetUnreadMessages( ref myId, ref
msgs).GetType().FullName);

this should show you the type name of the class being returned. From there
you should be able to cast the result to the right type and get access to
the properties.


Thank you, msgs.GetType().FullName returned System.Object[,] which did the
trick nicely.
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.