大家好,下面是我的代码,红色部分是出错的地方.请大家帮我纠正一下,不胜感激!using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace TestCollection
{
class ForeachFunction
{
static void Main(string[] args)
{
Vector someVector = new Vector();
foreach (double component in someVector)
{
Console.WriteLine("Component is " + component);
}
}
}
#region The rest of the part realize the function of "foreach".But some errors occured.
struct Vector : IEnumerable
{
public double x, y, z; public IEnumerator GetEnumerator()
{
return new VectorEnumerator(this);
} private class VectorEnumerator : IEnumerator
{
Vector theVector;
int location; public VectorEnumerator(Vector theVector)
{
this.theVector = theVector;
location = -1;
} public bool MoveNext()
{
++location;
return (location > 2) ? false : true;
}
public object Current
{
get
{
if (location < -1 || location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector[(uint)location];
}
}
public void Reset()
{
location = -1;
}
}
}
#endregion}
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace TestCollection
{
class ForeachFunction
{
static void Main(string[] args)
{
Vector someVector = new Vector();
foreach (double component in someVector)
{
Console.WriteLine("Component is " + component);
}
}
}
#region The rest of the part realize the function of "foreach".But some errors occured.
struct Vector : IEnumerable
{
public double x, y, z; public IEnumerator GetEnumerator()
{
return new VectorEnumerator(this);
} private class VectorEnumerator : IEnumerator
{
Vector theVector;
int location; public VectorEnumerator(Vector theVector)
{
this.theVector = theVector;
location = -1;
} public bool MoveNext()
{
++location;
return (location > 2) ? false : true;
}
public object Current
{
get
{
if (location < -1 || location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector[(uint)location];
}
}
public void Reset()
{
location = -1;
}
}
}
#endregion}
解决方案 »
- 还是关于excel操作的问题
- 获取Calendar控件的日期值的问题?
- 从wrox网站下载的C#入门经典第三版的代码,很多有Application.EnableRTLMirroring();都报错,不知道是什么原因?
- 新建一个安装部署项目,在生成时出现错误?
- 往access表里添加内容时,提交Insert语法错误!
- 如何把类型当作一个参数传递
- 邮件头分解的问题---急--再线 !!
- C#密码DES加密,将加密后的数据存到数据库里
- 怎样发布我的.net应用程序,使其在其他未装.net的机子上也能运行??多谢
- 在vc中ShellExecute可以调用可执行文件,请问在c#中用什么
- new ArrayList() 后怎么对ArrayList释放吗
- web service 里的方法调用时参数的问题
是这一句 public object Current
{
get
{
if (location < -1 ¦ ¦ location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector[(uint)location];
}
}
加个else看看
get
{
if (location < -1 ¦ ¦ location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
else
return theVector[(uint)location];
}
错误提示是:无法将带[]的索引应用于"TestCollection.Vector"类型的表达式!
return theVector[(uint)location];
的uint也有问题,迭代器只接受int
public object Current
{
get
{
if (location < -1 ¦ ¦ location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector[(uint)location];
}
}
public void Reset()
{
location = -1;
}
} 这部分,换成一个索引器,试试
private class VectorEnumerator : IEnumerator
{
Vector theVector;
int location;
readonly int count; public VectorEnumerator(Vector theVector)
{
this.theVector = theVector;
location = -1;
count = theVector.Count;
} public bool MoveNext()
{
++location;
return (location > count-1) ? false : true;
}
public object Current
{
get
{
if (location < -1 ¦ ¦ location > count-1)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector[(uint)location];
}
}
public void Reset()
{
location = -1;
}
}
struct Vector : IEnumerable
{
//....
public object this[uint index]
{
get
{
return xxx;
}
}
//....
}
{
get
{
if (location < -1 || location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
else if (location == 0)
{
return theVector.x;
}
else if (location == 1)
{
return theVector.y;
}
else
{
return theVector.z;
}
}
}
建议LZ多思考下。
本来没索引的,用所以一定出错。
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections; namespace TestCollection
{
class ForeachFunction
{
static void Main(string[] args)
{
Vector someVector = new Vector();
foreach (double component in someVector)
{
Console.WriteLine("Component is " + component);
}
}
} struct Vector : IEnumerable
{
public double x, y, z; public IEnumerator GetEnumerator()
{
yield return x;
yield return y;
yield return z;
}
} }
using System.Collections;namespace TestCollection
{
class ForeachFunction
{
static void Main(string[] args)
{
Vector someVector = new Vector(1,2,3);
foreach (double component in someVector)
{
Console.WriteLine("Component is " + component);
}
}
}
#region The rest of the part realize the function of "foreach".But some errors occured.
public struct Vector : IEnumerable
{
private double[] dd;
//public double x, y, z; public IEnumerator GetEnumerator()
{
return new VectorEnumerator(this);
}
public Vector(double x,double y,double z)
{ dd = new double[3];
dd[0] = x;
dd[1] = y;
dd[2] = z;
} private class VectorEnumerator : IEnumerator
{
Vector theVector;
int location; public VectorEnumerator(Vector theVector)
{
this.theVector = theVector;
location = -1;
} public bool MoveNext()
{
++location;
return (location > 2) ? false : true;
}
public object Current
{
get
{
if (location < -1 || location > 2)
{
throw new InvalidOperationException("The enumerator is either before the first element or " + "after the last element of the Vector");
}
return theVector.dd[(uint)location]; }
}
public void Reset()
{
location = -1;
}
}
}
#endregion}