[Mono-bugs] [Bug 27713][Wis] New - Enumerator Collection with the use of pattern matching
bugzilla-daemon@rocky.ximian.com
bugzilla-daemon@rocky.ximian.com
12 Jul 2002 15:22:36 -0000
Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.
Changed by mwh@sysrq.dk.
http://bugzilla.ximian.com/show_bug.cgi?id=27713
--- shadow/27713 Fri Jul 12 11:22:36 2002
+++ shadow/27713.tmp.9566 Fri Jul 12 11:22:36 2002
@@ -0,0 +1,136 @@
+Bug#: 27713
+Product: Mono/Runtime
+Version: unspecified
+OS: other
+OS Details: LFS
+Status: NEW
+Resolution:
+Severity:
+Priority: Wishlist
+Component: misc
+AssignedTo: mono-bugs@ximian.com
+ReportedBy: mwh@sysrq.dk
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL:
+Cc:
+Summary: Enumerator Collection with the use of pattern matching
+
+Description of Problem:
+I just followed a nice c# book on how to implement a Enumerator Collection
+without using the Interface approch, the program compiles, but does not run.
+
+
+Steps to reproduce the problem:
+Here is the program:
+using System;
+using System.Collections;
+
+class Foobar {
+ public static void Main() {
+ IntList list = new IntList();
+ list.Add (1);
+ list.Add (55);
+ list.Add (43);
+
+ foreach (int val in list)
+ Console.WriteLine ("Value = {0}", val);
+ }
+}
+
+class IntList {
+ int[] values = new int[10];
+ int allocated = 10;
+ int count = 0;
+ int revision = 0;
+
+ public void Add (int value) {
+ if (count+1 == allocated) {
+ int[] newValues = new int[allocated*=2];
+ for (int i = 0; i < count; i++) {
+ newValues[i] = values[i];
+ }
+ values = newValues;
+ }
+
+ values[count] = value;
+ count++;
+ revision++;
+ }
+
+ public int Count {
+ get { return count;}
+ }
+
+ void CheckIndex (int index) {
+ if (index >= count || index < 0)
+ throw new ArgumentOutOfRangeException ("Index value out of range");
+ }
+
+ public int this[int index] {
+ get {
+ CheckIndex (index);
+ return values[index];
+ } set {
+ CheckIndex (index);
+ values[index] = value;
+ revision++;
+ }
+ }
+
+ public IntListEnumerator GetEnumerator() {
+ return new IntListEnumerator (this);
+ }
+
+ internal int Revision {
+ get { return revision; }
+ }
+}
+
+class IntListEnumerator {
+ IntList intList;
+ int revision;
+ int index;
+
+ internal IntListEnumerator (IntList intList) {
+ this.intList = intList;
+ Reset();
+ }
+
+ public bool MoveNext() {
+ return ++index >= intList.Count ? false : true;
+ }
+
+ public int Current {
+ get {
+ if (revision != intList.Revision) {
+ throw new InvalidOperationException
+ ("Collection modified while enumerating");
+ }
+ return intList[index];
+ }
+ }
+
+ public void Reset() {
+ index = -1;
+ revision = intList.Revision;
+ }
+}
+
+Actual Results:
+WARNING **: unhandled exception System.NullReferenceException: "A null
+value was found where an object instance was required"
+in <0x000cb> .Foobar:Main ()
+
+Expected Results:
+Value = 1
+Value = 55
+Value = 43
+RESULT: 0
+
+How often does this happen?
+Everytime.
+
+Additional Information:
+Compiled/Ran with mono 0.12
+It compiled and Ran under MS winblows .. Dick /#mono tried it.