tags:

views:

900

answers:

3

I have the following method signature:

public void MyFunction(Object[,] obj)

I create this object:

List<List<Object>> obj = new List<List<Object>>;

Is there an easy way I can convert this to an Object[,]?


UPDATE:

The fact is I like to use Lists because I can easily add a new item. Is there a way I can declare my List<> object to fit this need? I know the number of columns in my Object[,] but not the number of rows.

A: 

To be blunt, the answer is no, not easily.

Perhaps you would like to edit your question to give us more background about why these declarations are needed and we can help you with your root problem?


Re your update:

I assume you cannot change the function you need to pass this into.

I don't see why you cannot just use an object[,] to begin with. This is my recommendation.

I doubt this will help you in your situation, but it might make some of the array working easier on you to start with. Do you know about the .ToArray() method on a List?

Geoffrey Chetwood
Yes, I know this method, but the only time I use it it's when I got a single dimension array and Yes I cannot change the function using Object[,] ... Is there a way I can declare an object[,] without specifing the size, because I don't know the number of row ...
Melursus
The best reference I can give you is http://authors.aspalliance.com/remas/ASP.NET/VFAQNET/DynamicArrays/You will certainly need a creative solution though.
Geoffrey Chetwood
+6  A: 

No. In fact, these aren't necessarily compatible arrays.

[,] defines a multidimensional array. List<List<T>> would correspond more to a jagged array ( Object[][] ).

The problem is that, with your original object, each List<Object> contained in the list of lists can have a different number of objects. You would need to make a multidimensional array of the largest length of the internal list, and pad with null values or something along those lines to make it match.

Reed Copsey
Looks like some of your text got eaten. You should use < and > instead of literal angle brackets.
sblom
Nevertheless, there's a good reason to want to do this, even if the source data (inside the nested Lists) is never able to be 'jagged'. The reason is that T[,] does not implement IEnumerable<T> but T[][] does... See this question: http://stackoverflow.com/questions/2562817/selecting-a-multi-dimensional-array-in-linq
rohancragg
+2  A: 

You're not going to get a very simple solution for this (i.e. a few lines). LINQ/the Enumerable class isn't going to help you in this case (though it could if you wanted a jagged array, i.e. Object[][]). Plain nested iteration is probably the best solution in this case.

public static T[,] To2dArray(this List<List<T>> list)
{
    if (list.Count == 0 || list[0].Count == 0)
        throw new ArgumentException("The list must have non-zero dimensions.");

    var result = new T[list.Count, list[0].Count];
    for(int i = 0; i < list.Count; i++)
    {
        for(int j = 0; j < list.Count; j++)
        {
            if (list[i].Count != list[0].Count)
                throw new InvalidOperationException("The list cannot contain elements (lists) of different sizes.");
            result[i, j] = list[i][j];
        }
    }

    return result;
}

I've included a bit of error handling in the function just because it might cause some confusing errors if you used it on a non-square nested list.

This method of course assumes that each List<T> contained as an element of the parent List is of the same length. (Otherwise you really need to be using a jagged array.)

Noldorin
I already got something like you, but I was hopping there was a quicker way of doing it, thx anyway your solution is more generic!
Melursus
No pun intended?
Samuel
Yeah, no problem. Although I could be wrong, you're not going to get anything significantly simpler than this. If you decide to use jagged arrays instead, then I could suggest a LINQ/extension method solution of course.
Noldorin
The only way you could make it simpler is if you want to use the max size and leave empty cells.
Samuel
And for the record, to jagged array is super easy: objects.Select(subObjects => subObjects.ToArray()).ToArray();
Samuel
@Samuel: Yeah, that would have been exactly what I would have suggested.
Noldorin
@Noldorin in order for it to compile you might want to advise readers to declare it as: public static T[,] To2dArray<T>(this List<List<T>> list) inside of a [public static class Extension]
rohancragg