Newbie question - accessing ArrayList members via index.

D

Damien Sawyer

Hi, I am having trouble understanding how to access the items in an
ArrayList. From MSDN, "(an ILIST) Represents a collection of objects
that can be individually accessed by index."

This is exactly what I'm trying to do with an ArrayList bound to a
datagrid, however am having troubles. The ArrayList contains
'Department' objects....

I've included snippets from the immediate window to show what I'm
trying to do...


1/ I'm pretty sure that it's an ArrayList that I'm dealing with

? ((ArrayList)this.dataGrid1.DataSource).GetType
((System.Collections.ArrayList)()).GetType+0 // method+offset

2/ It does contain items.
? ((ArrayList)this.dataGrid1.DataSource)
{Count=7}
[0]: {nhRegistration.Department}
[1]: {nhRegistration.Department}
[2]: {nhRegistration.Department}
... etc

3/ However there appears to be no indexer or other Method that I can
use.
? ((ArrayList)this.dataGrid1.DataSource)[1] // wish to get object 1 in
collection
error: object '(ArrayList)this.dataGrid1.DataSource' doesn't have an
indexer


Can someone please explain what I'm doing wrong?

Thanks in advance.



Damien Sawyer
 
D

Dmytro Lapshyn [MVP]

Hi Damien,

An ArrayList does have an indexer and the code you are typing in the
Immediate windows appears to be quite correct.
I'd suggest that the first thing you try is to try the same code snippet in
your program rather than in the Immediate window. I don't want to claim it
as an IDE bug, but I have had problems with indexers in the Immediate and
Watch windows, so as long as your code snippet successfully compiles and
works in the built executable, there is little to worry about.

The situation described might however prevent you from accessing a
particular item of the list, and you might have to add the whole ArrayList
instance to the Watch window and to drill down the items.
 

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