2

I have the following C# List:

List<Response> listAllData = new List<Response>();

listAllData.Add(new Response() {
    strId = propResponse.strId,
    strName = propResponse.strName
});

And then I am converting it to an array as such:

object[] array2 = listAllData.ToArray();

But when I'm trying to write data to a range using:

rngValues.Value = array2;

I'm getting an error

Exception from HRESULT: 0x800A03EC

I am (reasonably) certain this is b/c the resulting array2 is not actually a 2D multidimensional array, but instead an array-of-arrays.

So my question is... How do I get my listAllData into a 2D array?

It is not a "jagged array" meaning there are always 2 elements in each entry.

3
  • one question. how you are able to assign listAllData.ToArray() into object array. that would not even compile. (note you cant assign Response[] to object[] but an object. or you have to cast element by element.) Commented Oct 30, 2015 at 19:09
  • 4
    It's neither a 2d array or an array of arrays, it's 1d array of response objects Commented Oct 30, 2015 at 19:09
  • @Steve - yep, that's what i was missing... thanks! Commented Oct 30, 2015 at 20:01

1 Answer 1

5

resulting array2 is not actually a 2D multidimensional array, but instead an array-of-arrays.

No, it's a 1-D array of Response objects. If you want it in a 2-D array of objects (where the row is the two string properties from the source object) you'll have to build a loop (Linq does not support 2-D arrays):

object[,] array2 = new object[listAllData.Count,2];
for(int i = 0; i < listAllData.Count; i++)
{
     array2[i,0] = listAllData[i].strId;
     array2[i,1] = listAllData[i].strName;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.