请帮助更正arraylist的此代码.. [英] Please help to correct this code for arraylist..

查看:74
本文介绍了请帮助更正arraylist的此代码..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个ArrayList。但是在这个当我添加一些数据然后在Add();给出错误ArrayList不包含add()的定义。



我尝试过:



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System .Web;

使用System.Web.UI;

使用System.Web.UI.WebControls;



使用System.Collections;

使用System.Xml.Linq;



命名空间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(年 - 2011);

YearArray.Add(年 - 2012);

YearArray.Add(Year - 2013​​);

YearArray.Add(Year - 2014);

YearArray.Add(Year - 2015);

YearArray.Add(年 - 2016);

YearArray.Add(年 - 2017);

YearArray.Add(年 - 2018年);

YearArray.Add(年 - 2019年);

YearArray.Add(年 - 2020);

}

解决方案

为什么会这样?

你已经定义了一个名为的新类ArrayList 隐藏任何使用 System.Collections.ArrayList 而没有命名空间限定。

您的选择:

1.重命名你的班级不要冲突



2.明确指定框架类的完全限定名称:

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



3.使用更适合类型的公司llection:

列表<> YearArray =  new 列表<>(); 



我会做1和3。


我认为您的问题是您的页面/类的名称。你把它声明为ArrayList。因此,当你定义YearArray时,它会选择你的类型而不是System.Collections.ArrayList



您应该将YearArray定义为:



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





或不使用ArrayList作为页面名称。


它可以通过两种方式解决

1)更改aspx的名称页面名称从 ArrayList 到其他名称(例如ArrayList1),因为 ArrayList 名称由 ArrayList类使用本身。

2)修改下面的代码

  ArrayList YearArray =  new  ArrayList();  



as

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


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");
}

解决方案

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:

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

or
3. Use a more type-appropriate collection:

List<> YearArray = new List<>();


I'd do BOTH 1 and 3.


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.


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

ArrayList YearArray = new ArrayList();


as

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


这篇关于请帮助更正arraylist的此代码..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆