473,394 Members | 1,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 14671
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
2
by: Ben Bush | last post by:
I have a lis: ,,,,,] I want a code to test when the difference between the first element in the list of list is equal to or larger than 6, then move the previous lists to the end of the list....
6
by: Neelesh Bodas | last post by:
C++ guarantees that there exists a valid iterator beyond the last element of a container. However, such iterator might not be written or dereferenced. This is also true for arrays - C++ gurantees...
7
by: | last post by:
I'm using ASP.NET 2.0, C#, SQL Server 2005, and databound controls like the DataList and the GridView. Right now I'm using SqlDataSource as part of very quick and dirty site; I'm not looking to...
3
by: jm.suresh | last post by:
Hi, I have a list and I want to find the first element that meets a condition. I do not want to use 'filter', because I want to come out of the iteration as soon as the first element is found. I...
10
by: Chris Forone | last post by:
Hello Group, there is some memberfunc for std::valarray to return a pointer to the first element in the array. How do i use this? Thanx a lot. HAND Chris
7
by: Szabolcs Borsanyi | last post by:
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a) { int...
6
by: CSharper | last post by:
I am trying to use the following; I have an array with bunch of values in it. I am trying to find a value that contains part of the string I am passing eg string array = {"help","Csharp rocks"} ...
4
by: mab464 | last post by:
I have this code on my WAMP server running on my XP machine if ( isset( $_POST ) ) { for($i=0; $i<count($_POST);$i++) { if ($ans != NULL ) $ans .= ", " . $_POST ; // Not the first...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.