[corlib] Fixes array enumerator to match .net behaviour
[mono.git] / mcs / class / corlib / System / Array.cs
index a72d68e4738621fe272907f70d56b47ce864a686..d699446f2b5ce3aad52382b8900a6bde0a3161bd 100644 (file)
@@ -207,7 +207,7 @@ namespace System
                        // we just decr the size, so, 0 - 1 == FINISHED
                        const int FINISHED = -1;
                        
-                       Array array;
+                       readonly Array array;
                        int idx;
 
                        internal InternalEnumerator (Array array)
@@ -218,7 +218,6 @@ namespace System
 
                        public void Dispose ()
                        {
-                               idx = NOT_STARTED;
                        }
 
                        public bool MoveNext ()
@@ -312,12 +311,17 @@ namespace System
                public object GetValue (int index)
                {
                        if (Rank != 1)
-                               throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
-                       if (index < GetLowerBound (0) || index > GetUpperBound (0))
+                               throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
+
+                       var lb  = GetLowerBound (0);
+                       if (index < lb || index > GetUpperBound (0))
                                throw new IndexOutOfRangeException (Locale.GetText (
                                        "Index has to be between upper and lower bound of the array."));
 
-                       return GetValueImpl (index - GetLowerBound (0));
+                       if (GetType ().GetElementType ().IsPointer)
+                               throw new NotSupportedException ("Type is not supported");
+
+                       return GetValueImpl (index - lb);
                }
 
                public object GetValue (int index1, int index2)
@@ -335,12 +339,17 @@ namespace System
                public void SetValue (object value, int index)
                {
                        if (Rank != 1)
-                               throw new ArgumentException (Locale.GetText ("Array was not a one-dimensional array."));
-                       if (index < GetLowerBound (0) || index > GetUpperBound (0))
+                               throw new ArgumentException (SR.Arg_RankMultiDimNotSupported);
+
+                       var lb  = GetLowerBound (0);
+                       if (index < lb || index > GetUpperBound (0))
                                throw new IndexOutOfRangeException (Locale.GetText (
                                        "Index has to be >= lower bound and <= upper bound of the array."));
 
-                       SetValueImpl (value, index - GetLowerBound (0));
+                       if (GetType ().GetElementType ().IsPointer)
+                               throw new NotSupportedException ("Type is not supported");
+
+                       SetValueImpl (value, index - lb);
                }
 
                public void SetValue (object value, int index1, int index2)
@@ -689,8 +698,9 @@ namespace System
                {
                        public Object Current {
                                get {
-                                       if (_index < 0) throw new InvalidOperationException(SR.InvalidOperation_EnumNotStarted);
-                                       if (_index >= _endIndex) throw new InvalidOperationException(SR.InvalidOperation_EnumEnded);
+                                       if (_index < 0) throw new InvalidOperationException (SR.InvalidOperation_EnumNotStarted);
+                                       if (_index >= _endIndex) throw new InvalidOperationException (SR.InvalidOperation_EnumEnded);
+                                       if (_index == 0 && _array.GetType ().GetElementType ().IsPointer) throw new NotSupportedException ("Type is not supported");
                                        return _array.GetValueImpl(_index);
                                }
                        }