On Wed, 25 Oct 2006 07:24:38 +0200, Truong Hong Thi <th*****@gmail.com
wrote:
>
As the exception name suggests, it could be that index is out of range?
Is the Hashtable class something you are looking for?
Ant wrote:
>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).
--
Happy Coding!
Morten Wennevik [C# MVP]