471,594 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

copying to a multidimensional array?

I'm trying to copy data from a 1D array to a 2D array.
The obvious thing doesn't work:

int[,] twoDee = new int[2,2];
int[] oneDee = new int[2] { 1, 2 };
Array.Copy(oneDee, 2, twoDee, 2, 2);

This causes a RankException. But the MSDN documentation says:

When copying between multidimensional arrays, the array
behaves like a long one-dimensional array, where the rows
(or columns) are conceptually laid end to end.

This seems to suggest that there should be some way to accomplish
this, but I can't find any other syntax that's acceptable. I can
do it using a jagged 2D array, but I really wanted to avoid creating
lots of 1D subarray objects. Anybody know the trick?
Nov 16 '05 #1
1 7645
I think you misinterpreted what the documentation says. "When copying <b>between</b> multidimensional array<b>s</b>", that would suggest to me what you need both src and dest to be multidimensional. also, if you look at a list of exceptions the method throws, you would see it clearly states that RankException occurs when src and dest have different ranks

I don't know if there's a prebuilt way of doing what you want. but I would just write my own routine to do the copying

----- Mark Smith wrote: ----

I'm trying to copy data from a 1D array to a 2D array
The obvious thing doesn't work

int[,] twoDee = new int[2,2]
int[] oneDee = new int[2] { 1, 2 }
Array.Copy(oneDee, 2, twoDee, 2, 2)

This causes a RankException. But the MSDN documentation says

When copying between multidimensional arrays, the arra
behaves like a long one-dimensional array, where the row
(or columns) are conceptually laid end to end

This seems to suggest that there should be some way to accomplis
this, but I can't find any other syntax that's acceptable. I ca
do it using a jagged 2D array, but I really wanted to avoid creatin
lots of 1D subarray objects. Anybody know the trick

Nov 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Rob Tweed | last post: by
5 posts views Thread by TLOlczyk | last post: by
2 posts views Thread by chris | last post: by
1 post views Thread by Chuy08 | last post: by
5 posts views Thread by LittleCake | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.