Arrays of Multidimensional Arrays?

B

barmy_mad

Hi

I new to C# and trying to store/extract a collection of
multidimensional arrays. For example;

string[,] a_DATA = new string[10,3]
object[] o_CON = new object[10]

for(int i=0;i<10;i++){
for(int j=0;j<3;j++){
a_DATA[i,,j] = i + "x" + j
}
o_CON =a_DATA
}

How can the multidimensional array be extracted from o_CON[n]?

I hope you can help :)
 
B

Bruce Wood

The short answer is that o_CON is an array of objects. At run time, the
system will know what each object in the array really is, but the
compiler doesn't know, so you cast the object in the array to the type
you want. The cast is, in effect, a promise to the compiler that, "I
know what I'm doing. At run time this will, in fact, be an array. Trust
me":

string[,] arrayInOCon = (string[,])o_CON[2];

By the way, for something like o_CON, you might consider using an
ArrayList rather than an array, since the ArrayList will grow to
acommodate each new array that you add to it, whereas an array must be
created of the correct size up front.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

o_CON =a_DATA


You do realize that you are assigning the SAME value to all the elements of
o_CON right?

What is what you want to store in each element of o_CON?
How can the multidimensional array be extracted from o_CON[n]?

string[,] a = (string[,]) o_CON[x]
 
C

Clova

Hi Bruce

Many thanks for your prompt reply and clear answer. The object array
appears to be the only way C# will allow storing of an "array of
multi-dimensional arrays". Jagged or single arrays wouldn't work for me
so I'd switched to the flexible Object. The length is already known so
keeping with an array seemed more efficient.


Thanks again for your time :)
Barry
 
C

Clova

Hi Ignacio

The above snippet is only for example. The actual code doesn't assign
the array again and again I just thought that was an easy way to
demonstrate my problem.

Thanks for your reply and assistance :)

Barry
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

Here is an example. This is a console application for simplicity.

using System;
using System.Collections.Generic;
using System.Text;

namespace MiltidimArray
{
class Program
{
public Program()
{

string[,] a_DATA = new string[10,3];
object[] o_CON = new object[10];

for(int i=0;i<10;i++)
{
for(int j=0;j<3;j++)
{
a_DATA[i,j] = i + "x" + j;
}
o_CON =a_DATA;
}

// Read the data out.
for (int counter = 0; counter < 10; counter++)
{
string[,] a_DATA_local = (string[,])o_CON[counter];
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 3; j++)
{
System.Console.Write(a_DATA[i, j] + " ");
}
}
System.Console.WriteLine();
}
System.Console.ReadKey();
}

static void Main(string[] args)
{
new Program();
}
}
}


Regards,
Lars-Inge Tønnessen
 
B

Bruce Wood

The length is already known so keeping with an array seemed more efficient.

There's nothing wrong with using an array when the length is already
known. That's a fair design decision.

Nonetheless, don't do it for "efficiency" unless you know for a fact
that using a more appropriate data structure creates a bottleneck.
Always use the most appropriate data structure for the job, then tune
if you find problems. ArrayList, for example, uses an array internally,
so there's really not much difference between it and an array, unless
you're adding large numbers of items.

Anyway, in your case I don't think that advice applies: you know the
length up front. I just saw the word "efficient" and wanted to comment
on that. :)
 
C

Clova

Thanks guys, your help has been of much use. I appreciate your help and
welcome to the group.

Have a nice day!
Barry
 
L

Lars-Inge Tønnessen \(VJ# MVP\)

I appreciate your help and welcome to the group.

Thanks!

Regards,
Lars-Inge Tønnessen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top