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
Carl Daniel [VC++ MVP] wrote:
> "Zenon" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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
|