Thread: Getting an element from an ArrayList

  1. #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.

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    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.

  3. #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

  4. #4
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    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.

  5. #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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ArrayList Basics - Example
    By wbeasl in forum C# Programming
    Replies: 2
    Last Post: 12-26-2008, 04:41 PM
  2. Replies: 4
    Last Post: 01-05-2008, 11:30 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Sorting a 2-dimensional array
    By kmoyle73 in forum C++ Programming
    Replies: 3
    Last Post: 05-05-2004, 01:54 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM