3

So I need to use an ArrayList in C#. I looked it up in MSDN and they said I had to create a class, add System.Collections and Add

IList, ICollection, IEnumerable, 

ICloneable to the class

class clsExample : IList, ICollection, IEnumerable, ICloneable;

then I tried to use my Arraylist. So I typed:

ArrayList myAL = new ArrayList();

But the problem is that when I try to add things to the array (myAL.Add(1, Example);) the code doesn't find the array (myAL) and underlines an error to it. Am I missing something? Code:

    using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication7
{
    [SerializableAttribute]

    class clsAL : IList, ICollection, IEnumerable,
    ICloneable
    {
        ArrayList AL = new ArrayList();

    }
}
3
  • "Am I missing something? " - Yes, a short representative code snippet that displays your problem. Commented Nov 18, 2012 at 3:12
  • 1
    What code "doesn't find the array"? You're declaring it local to the clsAL class here, so external code wouldn't see this private member. Commented Nov 18, 2012 at 3:53
  • > So I need to use an ArrayList in C# ... and there was the first mistake. ArrayLists in C# are not good. Use a List<T> instead. Commented Nov 18, 2012 at 4:38

3 Answers 3

2

To use ArrayList you do not need to implement the Interfaces you are having. Remove them and add the object in ArrayList in some method as given below. If it is possible you should look into generic list that saves you from type casting.

namespace WindowsFormsApplication7
{     
    class clsAL
    {
        ArrayList AL = new ArrayList();
        public void YourFun()
        {
             AL.Add("First");
        }
    }
}

Edit: You can look this msdn example for understand how to use ArrayList

using System;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add("Hello");
      myAL.Add("World");
      myAL.Add("!");

      // Displays the properties and values of the ArrayList.
      Console.WriteLine( "myAL" );
      Console.WriteLine( "    Count:    {0}", myAL.Count );
      Console.WriteLine( "    Capacity: {0}", myAL.Capacity );
      Console.Write( "    Values:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.Write( "   {0}", obj );
      Console.WriteLine();
   }

}
Sign up to request clarification or add additional context in comments.

2 Comments

it doesn't matter because whenever I type "myAL" it underlines an error to it.
Well obviously myAL is not declared here. Either use the AL you declared or rename AL to myAL.
1

Unless you're forced to use ArrayList for some peculiar reason, I strongly advise using the generic List class instead.

1 Comment

to be honest I'm taking programming classes and the teacher is teaching us to make Arraylist Classes... it's pretty embarrassing to have to ask other people about this.
0

For one ArrayList.Add does not have a signature that matches what you are trying to add, it is just an object. The closest thing I can think of is to use the ArrayList.Insert Method like this. The other thing is to make sure that your ArrayList is in Scope where you are trying to use it.

AL.Insert(AL.Count,"Example");

or

Al.Add("Example"); // will append it to the ArrayList

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.