Array.BinarySearch fails

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a sorted array containing strings. I am iterating through the array and clearing the contents one by one using "array.BinarySearch" to find each element. So far so good. But the moment I come to the last element, the BinarySearch method fails.It gives me a return value which is negative although the element is very much there. I am debugging through the code and I find nothing wrong with it.Note that i am using the first overloaded method of BinarySearch which accepts the name of the array and the value
But I feel the problem lies somewhere in the fact that I am iterating backwards. This is because I am storing the names of the controls (in a panel) into the array and depending on these names, I am removing the controls one by one and setting each element to nothing in the array. This all works really fine, except when it comes the last iteration, ie the first element in the array, it fails .Foll. is a part of the code

//strPBNames = Array name declared at module level : Private strPBNames(0) As String
//panThumb = Panel which holds a series of pictureboxes (pb0, pb1,pb2 etc)
//blnSelectAll = If all the controls are selected by pressing the Select All button

strPBNames.Sort(strPBNames)
intControlCount = panThumb.Controls.Count - 1
For intctr = intControlCount To 0 Step -1
If blnSelectAll = True Then
RemoveHandler panThumb.Controls(intctr).Click, AddressOf pb_Click
RemoveHandler panThumb.Controls(intctr).MouseDown, AddressOf pb_MouseDown
panThumb.Controls.RemoveAt(intctr)
Else
If strPBNames.BinarySearch(strPBNames, panThumb.Controls(intctr).Name) >= 0 Then
intIndex = strPBNames.IndexOf(strPBNames, panThumb.Controls(intctr).Name)
RemoveHandler panThumb.Controls(intctr).Click, AddressOf pb_Click
RemoveHandler panThumb.Controls(intctr).MouseDown, AddressOf pb_MouseDown
panThumb.Controls.RemoveAt(intctr)
strPBNames.Clear(strPBNames, intIndex, 1)
End If
End If
Next

Can anybody tell me why this might be happening ?Thanks
 
Hi Yogi,

A wild guess - are you removing it before the clear method is called? If
so, it can't clear what isn't there.

Bernie Yaeger

Yogi21 said:
Hi
I have a sorted array containing strings. I am iterating through the array
and clearing the contents one by one using "array.BinarySearch" to find each
element. So far so good. But the moment I come to the last element, the
BinarySearch method fails.It gives me a return value which is negative
although the element is very much there. I am debugging through the code and
I find nothing wrong with it.Note that i am using the first overloaded
method of BinarySearch which accepts the name of the array and the value.
But I feel the problem lies somewhere in the fact that I am iterating
backwards. This is because I am storing the names of the controls (in a
panel) into the array and depending on these names, I am removing the
controls one by one and setting each element to nothing in the array. This
all works really fine, except when it comes the last iteration, ie the first
element in the array, it fails .Foll. is a part of the code :
//strPBNames = Array name declared at module level : Private strPBNames(0) As String
//panThumb = Panel which holds a series of pictureboxes (pb0, pb1,pb2 etc)
//blnSelectAll = If all the controls are selected by pressing the Select All button

strPBNames.Sort(strPBNames)
intControlCount = panThumb.Controls.Count - 1
For intctr = intControlCount To 0 Step -1
If blnSelectAll = True Then
RemoveHandler panThumb.Controls(intctr).Click, AddressOf pb_Click
RemoveHandler panThumb.Controls(intctr).MouseDown, AddressOf pb_MouseDown
panThumb.Controls.RemoveAt(intctr)
Else
If strPBNames.BinarySearch(strPBNames,
panThumb.Controls(intctr).Name) >= 0 Then
 
No..I dont think this is the problem..because when I am debugging it using a watch window, I can very much see the last element in the array. But it just ignores it like there is nothing there and comes out of the loop !!! Anyways, Thanks for taking the time out to go through the code and suggest the solution ! Can you think of anything else?
 
Back
Top