Using an array list

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, I'm using an array list as the collection for my indexer (_alPeople).
When i try to set _alpeople to a value at a certain index, I get an error. I
can't simply add the value as it needs to be at an index set by the user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married to
the value?

Thanks for any tips on this

Ant
 
As the exception name suggests, it could be that index is out of range?
Is the Hashtable class something you are looking for?
 
Ant, it sounds like you maybe need to use a Hashtable (version 1.x) -- or a
Dictionary (version 2.0) -- as it sounds like your users are defining the key
into the collection.

The array list fails because you're trying to set an index that it doesn't
contain -- say you create an ArrayList with capacity 10, and add 3 elements
to it -- its length is 3 and its index range is 0-2 inclusive. If you try to
set index 4 if fails because 4 is not between in 0-2, even though it has
capacity 10 (setting capacity just pre-allocates memory, a performance
enhancement).

Using a hashtable will allow you to maintain a list of key/value pairs where
the key is unique in the set and need not be sequential with other keys,
unlike an index.

Good luck! - Ken
 
As the exception name suggests, it could be that index is out of range?
Is the Hashtable class something you are looking for?
Hi, I'm using an array list as the collection for my indexer
(_alPeople).
When i try to set _alpeople to a value at a certain index, I get an
error. I
can't simply add the value as it needs to be at an index set by the
user.

the Set statement is this:

_people[index] = value; or even _people.Insert(index,value);

but these throw 'ArgumentOutOfRange' exception stating: Must be
non-negative
and less than the size of the collection.

How can I use an array list & keep track of the index the user married
to
the value?

Thanks for any tips on this

Ant

Hi Ant,

As Truong Hong Thi said, the index is out of range. An ArrayList grows
dynamically as you put items in it, but you can only use indexes between0
and the number of already inserted items - 1. If you try to insert to an
index above the size of the ArrayList the index will be out of range, as
the exception tells you.

If you use a HashTable or Dictionary (.net 2.0) you can ignore the actual
position of the items and just store an item with its corresponding index
by simply adding a new item.

If you need the items to be ordered by indexes, and absolutely want to use
an ArrayList (or List in .Net 2.0), then you need to make sure the size of
the ArrayList is large enough (for instance by adding empty items until
the size is correct).
 

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

Back
Top