473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Array.BinarySearch fails

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
2 1478
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: mike | last post by:
I have ListArray with number in Eg: 1, 1.456, 2.43, 4, 6.78 next i have a decimal variable containing one number EG: 1.786 Could someone please tell me how i find the "closest match" number...
2
by: Mario | last post by:
Hi there, I'm writing a piece of code with VS.Net 2003, Framework 1.1. And I can't make BinarySearch to work right. Look at this sample: Dim sexy() As String = {"-", "a", "b", "ba", "A",...
8
by: Sueffel | last post by:
Okay, I can certinatly use a For..Next loop to find the specified info I need, but I'm thinking the BinarySearch will be faster, if I can get it working LOL Anyhew, got an array, may have 5 or...
8
by: Jan | last post by:
I've got the following array: FileIn(Counter) How can i search the array for example "window" TIA
11
by: Bob Rock | last post by:
Hello, I have an array of strings and need to find the matching one with the fastest possible code. I decided to order the array and then write a binary search algo. What I came up with is the...
6
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.