Thread: ArrayList Basics - Example

  1. #1
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36

    ArrayList Basics - Example

    I cannot seem to even get started using an ArrayList using Visual Studio 2005. The dot operator does not provide the methods explained in the book I have like "add", "clear", etc....

    If I use "private ArrayList ab = new ArrayList(5);" below then I get "No overload for method 'ArrayList' takes '1' arguments" compile error!

    What am I doing wrong?

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ArrayList
    {
        public partial class ArrayList : Form
        {
            private ArrayList ab = new ArrayList();
    
            public ArrayList()
            {
                InitializeComponent();
            }
    
            private void ArrayList_Load(object sender, EventArgs e)
            {
                // ab.add("Test"); -- This .add method is not allowed by the studio - Why?
    
            }  // end Load
    
    
        } // end partial class
    
    } // end namespace ArrayList

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    ArrayList is old. Don't use it. Since C# 2.0 and the addition of generics, that is a poor collection to use. Try a list<> instead. To use a list in your code declare it as:

    Code:
    list<string> m_list = new list<string>();
    now you can add any string type to the list.

    Code:
    m_list.add("Hello World");
    ;

    By using generic conainer classes you generally don't have to cast, and you also get compile time errors if you try to do something wrong, versus it generally blowing up at runtime with the old object containers.
    Last edited by valaris; 12-26-2008 at 04:33 PM.

  3. #3
    Polar Fuzz
    Join Date
    Oct 2003
    Posts
    36
    Oh, I did not realize it was deprecated. Thanks for the tip, I did quickly try the list<> and it worked!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arraylist methods
    By Tiffanie in forum C++ Programming
    Replies: 5
    Last Post: 09-16-2008, 12:56 AM
  2. Trouble using ArrayList
    By stevespai in forum C# Programming
    Replies: 12
    Last Post: 07-31-2006, 11:17 AM
  3. Getting an element from an ArrayList
    By osal in forum C# Programming
    Replies: 4
    Last Post: 08-03-2005, 09:34 AM
  4. Win Api Basics...
    By Devil Panther in forum Windows Programming
    Replies: 19
    Last Post: 09-09-2004, 11:28 AM
  5. The Basics
    By Granger9 in forum Windows Programming
    Replies: 5
    Last Post: 09-13-2002, 05:12 PM