ArrayList of arrays

Z

Zenon

I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;
string yy = AL[0].SInstance[0].Y;

As I step through the code, I can see all of the struct instances in
the array, but I don't know how to access them. Any ideas?

thanks,
 
C

Carl Daniel [VC++ MVP]

Zenon said:
I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;

int xx = (AL[0] as S).X;

.... etc.

Three problems. First problem is that the result of the ArrayList indexer
is of type object, not S, so you need the explicit cast to make it work.
Second is that you're not putting arrays into the array list, you're putting
inidividual structs into the ArrayList. If you really mean to put arrays
in, then you need something along the lines of the code below. Third is
that you're trying to use the identifier 'SInstance' in a way that doesn't
make sense. It's just the name of a local variable, not a type, or field or
property of some type.

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance]);

int xx = (AL[0] as S[])[0].X;
string yy = (AL[0] as S[])[1].Y;

.... etc.

-cd
 
Z

Zenon

Sorry I mistyped my example, I was putting arrays into the arraylist as

AL.add(SInstance);
AL.add(SInstance);

The explicit cast is what I needed, thanks.

Is it bad form to do what I am doing here? I was hoping to avoid a
rewrite, but at the same time, I don't want to write kludgy code.


thanks again
Zenon said:
I have a function which returns array of structs. I need to create a
collection of those arrays and thought that an ArrayList would be a
good way to do this since the count is variable. The problem I am
having is that although the arrays are being properly stored in the
ArrayList, I cannot access the individual items in the arrays. For
instance I was hoping to do this:

public struct S
{
public int X;
public string Y;
}

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance[0]);
AL.add(SInstance[1]);

int xx = AL[0].SInstance[0].X;

int xx = (AL[0] as S).X;

... etc.

Three problems. First problem is that the result of the ArrayList indexer
is of type object, not S, so you need the explicit cast to make it work.
Second is that you're not putting arrays into the array list, you're putting
inidividual structs into the ArrayList. If you really mean to put arrays
in, then you need something along the lines of the code below. Third is
that you're trying to use the identifier 'SInstance' in a way that doesn't
make sense. It's just the name of a local variable, not a type, or field or
property of some type.

S[] SInstance = new S[17];

SInstance[0].X = 5;
SInstance[0].Y = "foo";

SInstance[1].X = 10;
SInstance[1].Y = "bar";

ArrayList AL = new ArrayList();
AL.add(SInstance]);

int xx = (AL[0] as S[])[0].X;
string yy = (AL[0] as S[])[1].Y;

... etc.

-cd
 
C

Carl Daniel [VC++ MVP]

Zenon said:
Sorry I mistyped my example, I was putting arrays into the arraylist
as

AL.add(SInstance);
AL.add(SInstance);

The explicit cast is what I needed, thanks.

Is it bad form to do what I am doing here? I was hoping to avoid a
rewrite, but at the same time, I don't want to write kludgy code.

There's nothing overtly wrong with doing this. It's not the greatest style,
as there are lots of ways to get it wrong (as you found). you might
consider wrapping this whole collection of arrays of structures into your
own custom collection class to reduce the likelihood of errors in correctly
accessing/updating the structure. If you can, move to .NET 2.0 (or are
you already using it?) which would allow you to use

List<S[]> (or even List<List<S>> if you prefer).

rather than the weakly typed ArrayList.

-cd
 

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