E
Eric Eggermann
I'm going batty trying to figure out why this is not working.
I'm trying to find all objects within a class derived from CollectionBase
which meet a certain criteria, but the search seems to be skipping some
objects. The following code is supposed to start searching the InnerList
from the beginning, and continue searching while there are still objects
that my comparer class says matches. In the code, comp is my comparer
object, and source is the object I'm looking for.
int from = 0;
int pos = 0;
while(pos >= 0)
{
pos = InnerList.BinarySearch(from, InnerList.Count - from, source,
comp);
if(pos > -1)
{
//.....Do some stuff with the object at index pos....
from = pos + 1;
}
}
Now with my test, if there is one object in the collection, it works. I get
that object. But if there is more than one, I don't get all. With the test
case of five objects, I get three, the first two are skipped over. I checked
and found that my comparer is not being called five times. The documentation
states that if BinarySearch finds more than one match, it only returns the
first one. This is not what I am seeing. It seems to be skipping over the
first two objects in my collection, based on the fact that my comparer is
only being called three times, while searching through 5 objects, all of
which should be hits. If I iterate through the collection, calling my
comparer myself, all five return tests return 0 as expected.
I'm not particularyly averse to writing my own search but I'd like to
understand why I don't see the documented behavior. Any ideas?
Eric
I'm trying to find all objects within a class derived from CollectionBase
which meet a certain criteria, but the search seems to be skipping some
objects. The following code is supposed to start searching the InnerList
from the beginning, and continue searching while there are still objects
that my comparer class says matches. In the code, comp is my comparer
object, and source is the object I'm looking for.
int from = 0;
int pos = 0;
while(pos >= 0)
{
pos = InnerList.BinarySearch(from, InnerList.Count - from, source,
comp);
if(pos > -1)
{
//.....Do some stuff with the object at index pos....
from = pos + 1;
}
}
Now with my test, if there is one object in the collection, it works. I get
that object. But if there is more than one, I don't get all. With the test
case of five objects, I get three, the first two are skipped over. I checked
and found that my comparer is not being called five times. The documentation
states that if BinarySearch finds more than one match, it only returns the
first one. This is not what I am seeing. It seems to be skipping over the
first two objects in my collection, based on the fact that my comparer is
only being called three times, while searching through 5 objects, all of
which should be hits. If I iterate through the collection, calling my
comparer myself, all five return tests return 0 as expected.
I'm not particularyly averse to writing my own search but I'd like to
understand why I don't see the documented behavior. Any ideas?
Eric