5

I'm not able to declare an ArrayList. Here's my code. (I'd much rather use Lists, but I'm just trying to understand the concept of an ArrayList).

private void button1_Click(object sender, EventArgs e)
{
    ArrayList salesTotals = new ArrayList();
    decimal[] decimalSales = { 1000m, 2000m, 3000m };

    foreach (decimal singleSales in decimalSales)
    {
        salesTotals.Add(singleSales);
    }
}

When I compile this, I get this error:

'ArrayList' is a 'namespace' but is used like a 'type'

I'm using the namespace System.Collections (not .Generic)

What is causing this and how do I fix it?

davidism
  • 121,510
  • 29
  • 395
  • 339
Ray
  • 3,409
  • 8
  • 33
  • 40

6 Answers6

6

One of the namespaces in your project is ArrayList. This is causing the conflict.

Try changing the namespace, or fully qualifying it like this:

System.Collections.ArrayList salesTotals = new System.Collections.ArrayList ();
agent-j
  • 27,335
  • 5
  • 52
  • 79
3

It sounds like you are using the ArrayList within a namespace itself called ArrayList. The symbol is resolving to the namespace definition, which is invalid in the symbol's context, causing the error you describe.

mdm
  • 12,480
  • 5
  • 34
  • 53
  • 2
    sigh... I just proved my absolute "newbness". In visual studio, since I was trying to test a basic ArrayList, I called the project "ArrayList", which resulted in using a namespace with the same name. Thanks for pointing this out @mdm. – Ray Jul 01 '11 at 20:03
  • No worries, it happens to the best of us :-) – mdm Jul 01 '11 at 20:03
1

Cause you have a namespace defined with the name ArrayList. Change the namespace name to something different.

Rahul
  • 76,197
  • 13
  • 71
  • 125
0

In my case I was saved the project name as ArrayList and when I was using ArrayList then it tells ArrayList is only namespace but not a type.

After changing the project name, now I am able to use ArrayList as a type. If you have taken the project name as ArrayList then you should have to change it to another name, I'm sure it will definitely work!

Divyang Desai
  • 7,483
  • 13
  • 50
  • 76
0

error is depend on .net core version. Path is right click on project=> Properties => choose .net core version in target framework. Arraylist cannot use in lower .net core version.

0

Is your project called ArrayList? That would do it. The compiler is finding a namespace called ArrayList. Try fully qualifying it ie new System.ArrayList();

therealmitchconnors
  • 2,732
  • 1
  • 18
  • 36