Click here to Skip to main content
15,867,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
How can I convert 2D array of C# objects(saMatrix) to 2D double array (dArray)?

Thanks.

Int32 iDim = 3;
object saMatrix = null;

- saMatrix {object[3, 3]} object {object[,]}
[0, 0] 11.0 object {double}
[0, 1] 12.0 object {double}
[0, 2] 13.0 object {double}
[1, 0] 21.0 object {double}
[1, 1] 22.0 object {double}
[1, 2] 23.0 object {double}
[2, 0] 31.0 object {double}
[2, 1] 32.0 object {double}
[2, 2] 33.0 object {double}
//TO
double[,] dArray = new double[iDim, iDim];
Posted
Updated 1-Jun-18 10:47am
Comments
Sergey Alexandrovich Kryukov 13-Apr-15 18:32pm    
The question makes no sense. It all depends on how you want to map those matrices and their elements.
—SA
oleg63 14-Apr-15 10:10am    
Yes, it does.
It's square matrix. Essentially it passed back to the C# client from COM interface as safearray of variants(which are double type).
On C# side, I see it as described above.
It's need to be converted in 2D matrix of double.
So, if you know the answer, please submit it below,
if don't, just do not pollute the forum with the useless answers.
Sergey Alexandrovich Kryukov 14-Apr-15 10:18am    
If you take the labor of explaining what you want to achieve properly, I'll probably answer.
—SA
oleg63 14-Apr-15 13:35pm    
It's fun to see you as "Top Expert in 24hrs".
Looks like answers like this work as well ;)
Sergey Alexandrovich Kryukov 14-Apr-15 15:17pm    
Fun? No they don't. You assume too much. You did not really read my posts to judge. Don't try to put responsibilities on your sloppiness to others. Please, nothing personal. If you provide reasonable information, I'll gladly give you very comprehensive answer as I usually do, despite of all your rudeness and lack of collaboration you demonstrate right now.
—SA

1 solution

I looked for ways to do it with Array.ConvertAll, but was having trouble.
But this should work.
This assumes you don't know the sizes of the array dimentions
C#
//Your original object
object saMatrix = new object[3, 3] {{ 11.0, 12.0, 13.0 },
                                    { 21.0, 22.0, 23.0 },
                                    { 31.0, 32.0, 33.0 } };

//Create an array of doubles the same size.
double[,] dArray = new double[((object[,])saMatrix).GetUpperBound(0) + 1,
                              ((object[,])saMatrix).GetUpperBound(1) + 1];

//Cast and fill each item
for (int x = 0; x <= ((object[,])saMatrix).GetUpperBound(0); x++)
{
   for (int y = 0; y <= ((object[,])saMatrix).GetUpperBound(1); y++)
   {
     dArray[x, y] = (double)((object[,])saMatrix)[x, y];
   }
}
 
Share this answer
 
Comments
oleg63 17-Apr-15 12:22pm    
Wendell D H,
Thank you very much. Everything is working.
You are the MAN of the day...
- Have a good day :)
Oleg.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900