472,099 Members | 2,276 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,099 software developers and data experts.

How to reference the first element in an arraylist

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

Dec 5 '06 #1
10 14607
The first element has an index of 0. Does that help?
Robin S.
--------------------------
<ga********@myway.comwrote in message
news:11*********************@16g2000cwy.googlegrou ps.com...
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

Dec 5 '06 #2
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
Dec 5 '06 #3
casting that was the missing piece of the jigsaw, thankyou both!

Gary.

Marc Gravell wrote:
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
Dec 5 '06 #4
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<InternetExplorerlist = new List<InternetExplorer>();

// populate

list[0].InteretExplorerInterfaceMember();

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 5 '06 #5
Anders thankyou that's very interesting.

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

Gary-

Anders Borum wrote:
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<InternetExplorerlist = new List<InternetExplorer>();

// populate

list[0].InteretExplorerInterfaceMember();

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 5 '06 #6
ga********@myway.com wrote:
Anders thankyou that's very interesting.

I'd always wondered what List<Tmeant when i'd seen it, now i know!
Generic classes are cool. Once they came out, I have not used
ArrayList since!

Chris

Dec 5 '06 #7
Generic classes are cool. Once they came out, I have not used
ArrayList since!
Truthfully, you never should. List<Tis 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.
Dec 5 '06 #8
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<Tdoes 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?

Dec 5 '06 #9
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<Tdoes 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.
Dec 5 '06 #10
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<ISomeInterfacelist = new List<ISomeInterface>();

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

Marc
Dec 5 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by ehames | last post: by
2 posts views Thread by Ben Bush | last post: by
6 posts views Thread by Neelesh Bodas | last post: by
3 posts views Thread by jm.suresh | last post: by
10 posts views Thread by Chris Forone | last post: by
6 posts views Thread by CSharper | last post: by
4 posts views Thread by mab464 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.