17

I have a C# WCF webservice which is called by two VB 6 project. The target VB project is sending to the client VB project a multidimensional array.

I want to convert the multidimensional array to a jagged array but i have no luck.

How can i find the number of olements in my object[,] to be able to initialize the jagged array ?

I want to follow the answer from this question but i don't have a GetLength method on my object.

I tried :

int firstElement = astrManTfrLetters.GetLength(0); int secondElement = astrManTfrLetters.GetLength(1); 

And i stuck here.

3
  • 2
    Show us what code you have so far. Commented Feb 24, 2014 at 12:28
  • 1
    So, clearly, the object you've got is not an array. What is it? Have you tried casting it? Post code. Commented Feb 24, 2014 at 12:28
  • if (astrManTfrLetters.GetType().ToString().Equals("System.Object[,]")) { object[,] tempAstrManTfrLetters = astrManTfrLetters as object[,]; } Commented Feb 24, 2014 at 14:33

3 Answers 3

22

Usually the solutions presented assume 0-based indices but that's not always the case, mainly if on the client you are dealing with object[,]'s for Microsoft Excel.

Here is a solution for any indices:

internal static class ExtensionMethods { internal static T[][] ToJaggedArray<T>(this T[,] twoDimensionalArray) { int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0); int rowsLastIndex = twoDimensionalArray.GetUpperBound(0); int numberOfRows = rowsLastIndex + 1; int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1); int columnsLastIndex = twoDimensionalArray.GetUpperBound(1); int numberOfColumns = columnsLastIndex + 1; T[][] jaggedArray = new T[numberOfRows][]; for (int i = rowsFirstIndex; i <= rowsLastIndex; i++) { jaggedArray[i] = new T[numberOfColumns]; for (int j = columnsFirstIndex; j <= columnsLastIndex; j++) { jaggedArray[i][j] = twoDimensionalArray[i, j]; } } return jaggedArray; } } 
Sign up to request clarification or add additional context in comments.

Comments

4

This worked for me and did not require any looping. It took a object[85000,26] and converted it to object[85000][26] in a little over a second.

object[,] obj2D = ... // Take my 2D array and cast it as a 1D array object[] obj1D = ((object[,]) obj2D).Cast<object>().ToArray(); // using linq, chunk the 1D array back into a jagged array Int32 j = 0; object[][] jagged = obj1D.GroupBy(x => j++ / obj2D.GetLength(1)).Select(y => y.ToArray()).ToArray(); 

Comments

3

By default, the C# produces the 0-based array. I have fine-tuned Pedro's solution as below:

internal static class ExtensionMethods { internal static T[][] ToJaggedArray<T>(this T[,] twoDimensionalArray) { int rowsFirstIndex = twoDimensionalArray.GetLowerBound(0); int rowsLastIndex = twoDimensionalArray.GetUpperBound(0); int numberOfRows = rowsLastIndex - rowsFirstIndex + 1; int columnsFirstIndex = twoDimensionalArray.GetLowerBound(1); int columnsLastIndex = twoDimensionalArray.GetUpperBound(1); int numberOfColumns = columnsLastIndex - columnsFirstIndex + 1; T[][] jaggedArray = new T[numberOfRows][]; for (int i = 0; i < numberOfRows; i++) { jaggedArray[i] = new T[numberOfColumns]; for (int j = 0; j < numberOfColumns; j++) { jaggedArray[i][j] = twoDimensionalArray[i + rowsFirstIndex, j + columnsFirstIndex]; } } return jaggedArray; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.