How to reference the first element in an arraylist

G

garyusenet

Hi I'm using the following to reference each 'element' (is this the
correct term) in an arraylist: -

foreach (InternetExplorer ie in ar)
{
...
}

Q. How do I reference the nth element in an array list? Specifically in
this example I want to reference the first element of the arraylist ar.


I've tried ar[1] but this doesnt give me access to any members of ie.

TIA

Gary
 
M

Marc Gravell

First - most containers in .Net are 0-based. Second, when using un-typed
containers you need to cast.

So

InternetExplorer ie = (InternetExplorer) ar[0];

Marc
 
A

Anders Borum

For.NET 2.0 you can use the generic implementation of the ArrayList. It's
called List<T>, meaning you can use a strongly typed list of
InternetExplorer.

List<InternetExplorer> list = new List<InternetExplorer>();

// populate

list[0].InteretExplorerInterfaceMember();
 
G

garyusenet

Anders thankyou that's very interesting.

I'd always wondered what List<T> meant when i'd seen it, now i know!

Gary-
 
C

Chris Dunaway

Anders thankyou that's very interesting.

I'd always wondered what List<T> meant when i'd seen it, now i know!

Generic classes are cool. Once they came out, I have not used
ArrayList since!

Chris
 
D

Dustin Campbell

Generic classes are cool. Once they came out, I have not used
ArrayList since!

Truthfully, you never should. List<T> is so much richer than ArrayList that
you should never use the latter again unless you are working on a .NET 1.0/1.1
project.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
G

garyusenet

When I first learnt about the ArrayList (about a week ago) I needed to
use it because I didn't know in advance the number of elements that I
wanted to store.

One other thing that struck me as useful with the arraylist was the
ability to store different types in the same arraylist. If you are
using a List<T> does this not preclude you from storing different types
in the list?

I am fairly new to programming but just thought that the ability to
store different types was kind of neat considering all the other
'stores' of data i've encountered allowed you to only store data of a
certain type.

Am i right in thinking this is a useful feature of the ArrayList or is
there a better way of storing different data types in one collection?
 
D

Dustin Campbell

When I first learnt about the ArrayList (about a week ago) I needed to
use it because I didn't know in advance the number of elements that I
wanted to store.

One other thing that struck me as useful with the arraylist was the
ability to store different types in the same arraylist. If you are
using a List<T> does this not preclude you from storing different
types in the list?

I am fairly new to programming but just thought that the ability to
store different types was kind of neat considering all the other
'stores' of data i've encountered allowed you to only store data of a
certain type.

Am i right in thinking this is a useful feature of the ArrayList or is
there a better way of storing different data types in one collection?

Well, I would question why you need this feature in the first place because
it might be indicitive of a weak design. But, if you really want it you can
just use List<object>. Then, you get all of the cool API features of List<T>
in weakly-typed collection.

Best Regards,
Dustin Campbell
Developer Express Inc.
 
M

Marc Gravell

Really? Most people would consider this a real downside, as it makes it very
hard to know how to treat each element. It also makes it very hard to
implement equality comparers or sorters.

A compromise here can be to use an interface (or base class) to store
objects with similar intent, i.e.

List<ISomeInterface> list = new List<ISomeInterface>();

You could use List<object> which would be more-or-less equivalent to
ArrayList (but with the extra 2.0 methods) - but it wouldn't be my first
choice.

Marc
 

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