473,893 Members | 1,909 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 (InternetExplor er 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 14714
The first element has an index of 0. Does that help?
Robin S.
--------------------------
<ga********@myw ay.comwrote in message
news:11******** *************@1 6g2000cwy.googl egroups.com...
Hi I'm using the following to reference each 'element' (is this the
correct term) in an arraylist: -

foreach (InternetExplor er 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

InternetExplore r ie = (InternetExplor er) 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

InternetExplore r ie = (InternetExplor er) 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
InternetExplore r.

List<InternetEx plorerlist = new List<InternetEx plorer>();

// populate

list[0].InteretExplore rInterfaceMembe r();

--
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
InternetExplore r.

List<InternetEx plorerlist = new List<InternetEx plorer>();

// populate

list[0].InteretExplore rInterfaceMembe r();

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
Dec 5 '06 #6
ga********@mywa y.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
2377
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
1838
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. that is: ,,,,,]
6
2074
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 that there exists one position available beyond the end of array. Does it give similar guarantee about the position before the first element - the position indicated by the iterator rbegin()? (For arrays as well as standard containers)
7
1762
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 tier things. I want the topmost/first data element in a particular control to NOT be displayed. This should be possible by either telling the renderer to hide the first element programmatically, or by deleting it from the data store that it has...
3
6030
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 have implemented it this way, may be, there should be a built in hiding somewhere in the standard libraries? def exists(iterable, condition): ''' Return the first element in iterble that meets the condition.
10
6323
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
1701
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 *b=(void*)a; int *c=a; /*...*/ } Now the questions come:
6
7686
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"} if (array.Contains<string>("Csharp")) { //here I want to get the actual string like "Csharp rocks" string str = array.First<string>(); //Here there is a way, I can
4
4569
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 element so append a comma
0
9987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11245
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10929
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9647
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8025
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5859
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6066
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4281
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.