Click here to Skip to main content
15,917,176 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
VB
For Each t As Test In m_AllTests
Dim name As String = t.Name.ToString.ToLower.Trim
intIndexLocation = Array.BinarySearch(m_key, name)
If intIndexLocation >= 0 Then


My array search works fine except for one item which can't be found. intIndexLocation is -1 when it tries to search for that item. I have run my program through the debugger and verified that the item is present in the array. Can someone suggest another function or another way to solving this problem? Thank you.
Posted
Updated 18-Jan-11 3:44am
v4

The most common cause of this sort of problem (with string comparison/searching) is that you have trimmed your search-key but the strings in the array may not be trimmed. You can use an overload that lets you pass in a custom comparer, see:

http://msdn.microsoft.com/en-us/library/4ba2bttb.aspx[^]
 
Share this answer
 
You should verify that
- The array in which you are searching is sorted, and
- The item you say is there but cannot be found is all lower case and trimmed.

You may want to post a larger fragment of your program, where you populate and sort m_key.
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 10:04am    
5+ Probable cause
Is your array sorted?! (just in case)
As BinarySearch required sorted array to work properly.
 
Share this answer
 
Comments
Espen Harlinn 18-Jan-11 10:04am    
5+ Probable cause

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900