By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,110 Members | 990 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,110 IT Pros & Developers. It's quick & easy.

Array.BinarySearch fails

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <an*******@discussions.microsoft.com> wrote in message
news:E8**********************************@microsof t.com...
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.

Nov 20 '05 #2

P: n/a
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?
Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.