请帮我检错 关于C# 集合程序

my_ibm 2004-10-02 05:40:00
请帮我检查并调试一下
出错信息:
E:\Visual Studio Projects\ConsoleApplication8\Class1.cs(50): 找不到类型或命名空间名称“collection”(是否缺少 using 指令或程序集引用?)


using System;
using System.Collections ;



/// <summary>
/// Class1 的摘要说明。
/// </summary>


namespace guopeixin
{

public class MyCollection
{
string[] items;

//初始化此类
public MyCollection()
{
items = new string [5] {"guopw","oii n","jfgj","flka","jfkja"};
}



//定义此类必需的方法GetEnumerator
public MyEnumerator GetEnumerator()
{
return new MyEnumerator(this);
}


//声明内部类MyEnumerator
public class MyEnumerator
{
int nindex;
public MyEnumerator(MyCollection cell)
{
MyCollection collection = cell;
nindex =-1;


}

//声明集合必需的方法MoveNext

public bool MoveNext()
{
nindex++;
return ((nindex< collection.items.GetLength()));
}
public string Current
{
get
{

return collection.items[nindex];

}
}
}

public static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//

MyCollection co = new MyCollection();
Console.WriteLine("Value\n");
foreach(string guopei in co)
{
Console.WriteLine ("{0}",guopei);

}
}



}
}


本文地址:
http://bbs.qq.com/cgi-bin/bbs/show/content_div?club=3&groupid=102:10685&messageid=1219

...全文
75 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
my_ibm 2004-10-03
  • 打赏
  • 举报
回复

结贴.
谢谢popcorn(米花),我可是搞了好几天都没找出错误.
popcorn 2004-10-02
  • 打赏
  • 举报
回复
你声明collection的位置不正确,要在外面声明,请修改如下地方:
MyCollection collection;
public MyEnumerator(MyCollection cell)
{
collection = cell;
nindex =-1;
}

还有没有GetLength:
public bool MoveNext()
{
nindex++;
return ((nindex< collection.items.Length));
}

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧