Click here to Skip to main content
15,939,853 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to make a ArrayList. But in this When i add some data then on Add(); give error "ArrayList Does not contain a definiation for add()".

What I have tried:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Collections;
using System.Xml.Linq;

namespace UI
{
public partial class ArrayList : System.Web.UI.Page
{
ArrayList YearArray = new ArrayList();

protected void Page_Load(object sender, EventArgs e)
{

}

private void FillArrayList()
{

YearArray.Add("Year - 2010");
YearArray.Add("Year - 2011");
YearArray.Add("Year - 2012");
YearArray.Add("Year - 2013");
YearArray.Add("Year - 2014");
YearArray.Add("Year - 2015");
YearArray.Add("Year - 2016");
YearArray.Add("Year - 2017");
YearArray.Add("Year - 2018");
YearArray.Add("Year - 2019");
YearArray.Add("Year - 2020");
}
Posted
Updated 14-Apr-16 11:44am

Why would it?
You have defined a new class named ArrayList that "hides" any use of the System.Collections.ArrayList without namespace qualification.
Your choices:
1. Rename your class not to conflict
or
2. Explicitly specify the fully qualified name for the framework class:
C#
System.Collections.ArrayList YearArray = new System.Collections.ArrayList();
or
3. Use a more type-appropriate collection:
C#
List<> YearArray = new List<>();

I'd do BOTH 1 and 3.
 
Share this answer
 
I think your problem is the name of your page/class. You have it declared as ArrayList. So when you are defining YearArray is is picking that up as your type and not System.Collections.ArrayList

You should either define YearArray as:

System.Collections.ArrayList YearArray = new System.Collections.ArrayList();


or not use ArrayList as the name of your page.
 
Share this answer
 
it can be resolved in two ways
1) change the name of your aspx page name from ArrayList to some other name (eg ArrayList1 ), since ArrayList name is used by the ArrayList class Itself.
2) modify the below code
C#
ArrayList YearArray = new ArrayList();

as
C#
System.Collections.ArrayList YearArray = new System.Collections.ArrayList();
 
Share this answer
 
v2
To start with, forget ArrayList. This class is badly obsolete. It's not formally marked obsolete, because it will work just fine in legacy code, but using it in any development makes no sense at all. Non-generic (but not specialized) collection classes have been rendered obsolete as early as of .NET v.2.0, when generics were introduced. You don't want to deal with untyped list items. So, just forget ArrayList.

Instead, use System.Collection.Generic.List<> or, for strings only, maybe System.Collections.Specialized.StringCollection.

Other solutions resolve your bug.

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900