C Board  

Go Back   C Board > General Programming Boards > C# Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2005, 02:31 PM   #1
Registered User
 
Join Date: May 2004
Posts: 215
Getting an element from an ArrayList

Lets say I wanted to get each element from an ArrayList, how could that be done.

Im trying to use a for loop, but I cannot figure out how to get the element out of the ArrayList.
osal is offline   Reply With Quote
Old 08-02-2005, 03:01 PM   #2
and the Hat of Clumsiness
 
GanglyLamb's Avatar
 
Join Date: Oct 2002
Posts: 1,101
I think http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemcollectionsarraylistclassitemtopic.asp

see the "Item" property.

is what you need. MSDN is really good to explore and get familiar with most classes.
__________________
GanglyLamb is offline   Reply With Quote
Old 08-02-2005, 03:34 PM   #3
Registered User
 
Join Date: Jan 2004
Posts: 37
If you know that all the elements are of the same type in the array list and you know what that type is then you could use the foreach keyword.

Example:

Code:
//ArrayList contains Control objects

foreach (Control c in myArrayList)
{
c.Visible = false;
}
LT
CompURocket is offline   Reply With Quote
Old 08-03-2005, 01:39 AM   #4
the hat of redundancy hat
 
nvoigt's Avatar
 
Join Date: Aug 2001
Location: Hannover, Germany
Posts: 2,769
Actually, you don't even need to know this at compile time

Code:
//ArrayList contains Control objects
foreach ( object o in myArrayList )
{
    MessageBox.Show( o.ToString() );
}
__________________
hth
-nv

She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

When in doubt, read the FAQ.
Then ask a smart question.
nvoigt is offline   Reply With Quote
Old 08-03-2005, 09:34 AM   #5
Registered User
 
Join Date: Jan 2004
Posts: 37
Well, you do have to know the type if you want to do anything with it other than call ToString() and the other methods offered by System.Object.

LT
CompURocket is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ArrayList Basics - Example wbeasl C# Programming 2 12-26-2008 04:41 PM
Exercise asks me to use a subarray. What is a subarray? yougene C Programming 4 01-05-2008 11:30 PM
Modify an single passed array element swgh C Programming 3 08-04-2007 08:58 AM
Sorting a 2-dimensional array kmoyle73 C++ Programming 3 05-05-2004 01:54 PM
Struct *** initialization Saravanan C Programming 20 10-09-2003 12:04 PM


All times are GMT -6. The time now is 09:50 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22