[Mono-bugs] [Bug 529927] New: TreeNodeCollection.Contains() appears to be broken

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Aug 11 05:58:08 EDT 2009


http://bugzilla.novell.com/show_bug.cgi?id=529927


           Summary: TreeNodeCollection.Contains() appears to be broken
    Classification: Mono
           Product: Mono: Class Libraries
           Version: 2.4.x
          Platform: x86
        OS/Version: All
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Windows.Forms
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: jxelam at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---


Description of Problem:

TreeNodeCollection.Contains() returns false even if the collection does indeed
contain the given node.

Steps to reproduce the problem:
1. Compile and run the program below (-R:System.Windows.Forms)

using System;
using System.IO;
using System.Windows.Forms;

class TreeContainsFail {
    static void Main(string[] args) {
        TreeView tree = new TreeView();
        TreeNodeCollection nodes = tree.Nodes;
        TreeNode node = new TreeNode();

        nodes.Add(node);
        Console.WriteLine(nodes.Contains(node));
    }
}

Actual Results:

False

Expected Results:

True

How often does this happen? 

Every time

Additional Information:

The function itself:

        public bool Contains (TreeNode node)
        {
            return (Array.BinarySearch (nodes, node) > 0);
        }

relies on Array.BinarySearch which passes to:
public static int BinarySearch<T> (T [] array, int index, int length, T value,
IComparer<T> comparer);

At a glance, it appears
               else if (iCmp < 0)
should be:
               else if (iCmp > 0)

like all the other references.

Even with this changed, an exception is thrown due to TreeNode not implementing
IComparable (which I guess would have been missed if due to the above the node
is always null).

A quick fix I implemented was to simply iterate through the array checking the
reference:

        public bool Contains (TreeNode node)
        {
            for (int i = 0; i < count; i++)
              if (nodes[i] == node) return true;

            return false;
        }

as
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenodecollection.contains.aspx
states that "This method checks for reference equality only. You cannot use it
to determine whether an equivalent but different node is in the collection."

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list