468,553 Members | 1,339 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,553 developers. It's quick & easy.

casting Object to byte array

I have a COM server that is returning a type Object that I need to cast as a
byte array. Can anyone tell me how this is performed in C#?

Thanks,

glenn
Nov 17 '05 #1
3 52865
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.

glenn
"glenn" <gh******@softeksoftware.com> wrote in message
news:OR**************@TK2MSFTNGP09.phx.gbl...
I have a COM server that is returning a type Object that I need to cast as a byte array. Can anyone tell me how this is performed in C#?

Thanks,

glenn

Nov 17 '05 #2
glenn <gh******@softeksoftware.com> wrote:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was
getting errors.


That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #3
thanks

glenn

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
glenn <gh******@softeksoftware.com> wrote:
Nevermind, I think I figured it out, here is what I did so anyone can
correct me if I am going to find a problem with this later on...

Byte [] b = new byte[1023];
b = (byte [])myobj;

The problem was I was not adding the square brackets in my casting and was getting errors.


That's fine, except you're creating a new byte array for no reason -
just generating garbage.

Just use:

byte[] b = (byte[]) myobj;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by David Cook | last post: by
6 posts views Thread by Joe Thompson | last post: by
3 posts views Thread by Fireangel | last post: by
4 posts views Thread by gg9h0st | last post: by
20 posts views Thread by quantumred | last post: by
12 posts views Thread by O.B. | last post: by
10 posts views Thread by Scott Townsend | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by UniDue | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.