大家好,下面是我的代码,红色部分是出错的地方.请大家帮我纠正一下,不胜感激!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}

解决方案 »

  1.   

    不好意思,没有变成红色!
    是这一句      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]; 
                    } 
                }
      

  2.   

    LZ什么错误提示?
    加个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]; 
                    } 
      

  3.   

    加了else了,还是一样的!
    错误提示是:无法将带[]的索引应用于"TestCollection.Vector"类型的表达式!
      

  4.   

    Vector 还要继承一下IList接口,同时
                        return theVector[(uint)location];  
    的uint也有问题,迭代器只接受int
      

  5.   

    晕啊,如果要继承IList接口,那么还要实现这个接口中的那么多方法啊!我这个程序也就是模仿一下foreach的内部功能!那么按照你的思路,你能给我一个完整的代码吗?
      

  6.   

    网络上很多,你搜一下IList就有无数的结果
      

  7.   

    楼主   把
     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; 
                } 
            } 这部分,换成一个索引器,试试
      

  8.   

    错误应该是你的程序中使用的“2”不正确,因为它有可能大于theVector的个数。你试一试这样写:
    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;
                }
            } 
      

  9.   

    //需实现一个取索引值的方法
    struct Vector : IEnumerable
    {
    //....
        public object this[uint index]
        {
            get
            {
                return xxx;
            }
        }
    //....
    }
      

  10.   

    Current部分这么写 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"); 
                        }
                        else if (location == 0)
                        {
                            return theVector.x;
                        }
                        else if (location == 1)
                        {
                            return theVector.y;
                        }
                        else
                        {
                            return theVector.z;
                        }
                    } 
                } 
    建议LZ多思考下。
    本来没索引的,用所以一定出错。
      

  11.   

    可以用yield简化代码
    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;
            }         
        } }
      

  12.   

    http://hi.baidu.com/youniao/blog/item/d3448d2fbe0e4a3b1f308922.html
      

  13.   

    楼主,这是我改过后的代码,不知道是不是你想要的 :using System;using System.Text;
    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}