Array of arrays

K

Kurzweil

I need to make a two dimensional array of objects. These objects are of type
Influence[].
How do I declare such an array?

Now I use:
private object[,] influences;
influences[x, y] = GetInfluences(); // GetInfluences returns type
Influence[]

But when I need to use something from te array I have to typecast from
object to Influence[].
I would like to have a two dimensional array of type Influence[] in stead of
type object.

private Influence[][,] influences; // This does compile
influences[x, y] = GetInfluences(); // Compile error : Wrong number of
indices inside [], expected '1'

Does anyone have an idea?

Thanks in advance,
Kurzweil
 
M

mk

Hi,
Look at your individual declarations, where you create
the initial object array, you declare it as [,]. Where
you create the Influences array, you declare it as [][,]!
Unless you've changed the GetInfluences() method, you're
not going to return the correct number of dimensions.
I suspect that this is just a syntax error on your part.

HTH,

martin
 
J

Jon Skeet

I would like to have a two dimensional array of type Influence[] in stead of
type object.

private Influence[][,] influences; // This does compile
influences[x, y] = GetInfluences(); // Compile error : Wrong number of
indices inside [], expected '1'

Does anyone have an idea?

Yes - you've declared it the wrong way round. You meant:

private Influence[,][] influences;
 
O

Oscar Papel

Hello,

I would suggest that you create a strongly typed collection to help simplify
things.

I would suggest two collections in this case:
InfluenceMatrix and InfluenceCollection
InfluenceMatrix would hold only InfluenceCollection objects
That way, your code would look like this:

private InfluenceMatrix influences=new InfluenceMatrix();
influences[x,y]=GetInfluences() // change GetInfluences to return
InfluenceCollection instead of Influence[]

There are many tools that generate strongly typed collections if you don't
want to write the boilerplate code yourself.
This allows you to separate interface from implementation in this case.
It also simplifies passing the data around in your program.
Finally, the collections themselves can be implemented using arrays so that
you have an O(1) get/set just like an array.
It will also simplify your documentation and make the users of the
collections much happier.

Oscar Papel.
 

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