[Mono-bugs] [Bug 54927][Wis] New - ArrayList::IndexOf does .Equals on the wrong object
bugzilla-daemon@bugzilla.ximian.com
bugzilla-daemon@bugzilla.ximian.com
Fri, 27 Feb 2004 08:05:12 -0500 (EST)
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 bmaurer@users.sf.net.
http://bugzilla.ximian.com/show_bug.cgi?id=54927
--- shadow/54927 2004-02-27 08:05:12.000000000 -0500
+++ shadow/54927.tmp.6592 2004-02-27 08:05:12.000000000 -0500
@@ -0,0 +1,125 @@
+Bug#: 54927
+Product: Mono/Class Libraries
+Version: unspecified
+OS:
+OS Details:
+Status: NEW
+Resolution:
+Severity:
+Priority: Wishlist
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com
+ReportedBy: bmaurer@users.sf.net
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL:
+Cc:
+Summary: ArrayList::IndexOf does .Equals on the wrong object
+
+As seen on mono-devel-list
+From Tom Shelton <tom@mtogden.com>:
+
+I have a case of some code I wrote using Mono, that doesn't work properly
+under .NET. I thought maybe you guys would like to know about it, as it
+seems to be a difference in implementation of the ArrayList class...
+
+Basically, what I have created is a typed collection class that inherits
+from
+System.Collections.CollectionBase. The type that is stored in this
+collection has overriden Object.ToEquals to look something like this:
+
+public class Column
+{
+ .....
+
+ public override bool ToEquals(object obj)
+ {
+ if (obj is Column)
+ {
+ return (obj.ToString() == this.ToString());
+ }
+ else if (obj is string)
+ {
+ return ((string) obj == this.ToString());
+ }
+ else
+ {
+ return false;
+ }
+ }
+
+}
+
+In my ColumnCollection class, I was implementing overloaded versions of
+the
+indexer, Contains, IndexOf, Remove, etc. that can take either another
+Column,
+or a string. This works beautifully under Mono...
+
+public class CollumnCollection : System.Collections.CollectionBase
+{
+ ....
+
+ public int IndexOf (Column value)
+ {
+ return this.InnerList.IndexOf(value);
+ }
+
+ public int IndexOf (string value)
+ {
+ return this.InnerList.IndexOf(value);
+ }
+
+ etc...
+}
+
+When, I would do
+ColumnCollection columns = new ColumnCollection();
+
+columns.Add(new Column("TABLE", "FIELD"));
+....
+
+Column col = new Column("TABLE", "FIELD");
+Console.WriteLine(columns.Contains(col));
+Console.WriteLine("TABLE.FIELD");
+
+the output, would be:
+True
+True
+
+This does not work on .NET - what I get is:
+True
+False
+
+In stepping through the code this morning, I found that in .NET my .Equals
+method is never called. What I believe (no, I didn't look at the IL - I
+guess I should of), is that .NET is doing a sequence something like this
+when
+you call Contains, IndexOf, etc... (value is the object your looking
+for...)
+
+foreach (object obj in InnerList)
+{
+ if (value.Equals(obj))
+ {
+ .....
+ }
+}
+
+Where, as Mono seems to be doing something like this (and I think this is
+the
+more sensible approach by the way...)
+
+foreach (object obj in InnerList)
+{
+ if (obj.Equals(value))
+ {
+ ....
+ }
+}
+
+I fixed these string methods easily enough - I just did my own loop,
+rather
+then delegate to the InnerList. But, I thought I would point out this
+simple
+little inconsistency.