locked
Accessing object in an ArrayList

    Question

  • Hi
    I am writing a small program using Visual c# 2008 express. I have read in a book where I can access the methods of
    object that I have placed into an arraylist. For example

    Student name = new Student();
    studentList[0] = name;

    and if the object has a method such as GetAge(); I could use the following line to access the method

    studentList[0].GetAge();

    When I instantiate the objects and place them into the arraylist, this all seems to work fine.

    But when I want to access an array element, use its method as above,
    the intellisense does not give me the option to do so. I am sure I have
    all the correct permissions within the Student class.
    Am I missing something?
    Thanks
    Jason

    Thursday, April 16, 2009 12:34 PM

Answers

  • ArrayLists are a collection of objects. When you want to use an object inside the list, there is no way to infere what kind of object you are working with. You have 2 possible solutions:

    One is to cast the object to the propper type: ((Studen)studentList[0]).GetAge();

    The other is to used a typed collection, such as an array (Student[]) or a List (List<Student>).

    Hope this helps.
    /* No comments */
    • Marked as answer by jasno Thursday, April 16, 2009 1:52 PM
    Thursday, April 16, 2009 12:50 PM

All replies

  • Thats because the ArrayList would convert your Student Type into an Object type. You have to explicitly cast it as a Student Type

    Try the following:-

    Student _StuObj = (Student)StudentList[0];
    
    _StuObj.GetAge();
    
    
    now the Intellisense should work fine.
    Ganesh Ranganathan
    [Please mark the post as answer if you find it helpful]
    Thursday, April 16, 2009 12:49 PM
  • ArrayLists are a collection of objects. When you want to use an object inside the list, there is no way to infere what kind of object you are working with. You have 2 possible solutions:

    One is to cast the object to the propper type: ((Studen)studentList[0]).GetAge();

    The other is to used a typed collection, such as an array (Student[]) or a List (List<Student>).

    Hope this helps.
    /* No comments */
    • Marked as answer by jasno Thursday, April 16, 2009 1:52 PM
    Thursday, April 16, 2009 12:50 PM
  • If you check the ArrayList.Add method documentation, you can see that it takes objects in. In other words, when you pass any Student class to the ArrayList.Add method, the ArrayList will treat it as an object class (NOT student class). You shoud cast the object in some specific index to a Student class in order to access GetAge method ( example: ((Student)studentList[0]).GetAge();).

    Better option would be that you replace the ArrayList with strongly typed List<Student> (create it with List<Student> studentList = new List<Student>();).
    That collection will only accept student classes inside, otherwise your application will not compile. With strongly typed List<Student> list you can access your Student objects like in your question: studentList[0].GetAge();
    Tomi Airaksinen - MCPD [Remember to click "mark as answered" when you get a correct reply to your question]
    Thursday, April 16, 2009 12:53 PM
  • ArrayList students = new ArrayList();
    
    Student newStudent = new Student();
    
    students.Add(newStudent)
    
    /// To get the student object
    
    Student stud = (Student)students[0];
    stud.GetAge();
    
    
    /// If we use List then we dont need to cast
    
    List<Student> students = new List<Student>();
    Student newStudent = new Student();
    students.Add(newStudent)
    
    Student stud = students[0];
    stud.GetAge();

    Thanks, A.m.a.L | [Remember to click "mark as answered" when you get a correct reply to your question]
    Thursday, April 16, 2009 1:00 PM
  • So simple.
    Thank you all for the help
    Jason

    Thursday, April 16, 2009 1:53 PM
  • In addition to the above, take a look at System.Collections.Generic.  In most cases, you will be better off using a strongly typed Generic Collection rather than an arraylist.
    Thursday, April 16, 2009 2:02 PM
  • Now we are in .NET 3.5, ArrayLists, HashTables are somewhat deprecated. Use the generic lists whenever possible.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    Thursday, April 16, 2009 4:29 PM