PC Review


Reply
Thread Tools Rate Thread

ArrayList of arrays

 
 
Zenon
Guest
Posts: n/a
 
      14th Dec 2006
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,

 
Reply With Quote
 
 
 
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      14th Dec 2006
"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


 
Reply With Quote
 
Zenon
Guest
Posts: n/a
 
      14th Dec 2006
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


 
Reply With Quote
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      15th Dec 2006
Zenon wrote:
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Thread-safety: Change property of items in arraylist versus removingitems from the arraylist Curious Microsoft Dot NET 2 6th Aug 2008 12:36 PM
Arraylist of arrays wolima Microsoft C# .NET 3 6th Nov 2007 11:30 PM
ArrayList(ICollection) constructor & overriden ArrayList.AddRange(). Sylvain Microsoft C# .NET 1 4th Jun 2005 01:19 AM
ArrayList vs Arrays - Performance =?Utf-8?B?RGVubmlz?= Microsoft VB .NET 5 17th Aug 2004 01:59 AM
Using Arraylist with arrays Vaden95 Microsoft Dot NET 2 8th Jul 2003 07:18 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:41 PM.