Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > array searching


Reply
 
Thread Tools Display Modes
  #1  
Old 07-11-2004, 03:32 PM
davcaw's Avatar
davcaw davcaw is offline
Centurion
 
Join Date: Mar 2004
Location: Cedarburg, WI, USA
Posts: 138
Default array searching


i have a string and an array of strings. I want to see if the array contains the string and if it does, get the index. I tried array.binarysearch(), but it always returns -2001 whether it exists or not.
Reply With Quote
  #2  
Old 07-11-2004, 03:59 PM
OnErr0r's Avatar
OnErr0r OnErr0r is offline
Obsessive OPtimizer

Administrator
* Guru *
 
Join Date: Jun 2002
Location: Debug Window
Posts: 13,685
Default

Post a code snippet of how you're doing this.
__________________
Quis custodiet ipsos custodues.
Reply With Quote
  #3  
Old 07-11-2004, 05:17 PM
robM robM is offline
Newcomer
 
Join Date: Jul 2004
Posts: 17
Default The easy way would be...

Quote:
Originally Posted by davcaw
i have a string and an array of strings. I want to see if the array contains the string and if it does, get the index. I tried array.binarysearch(), but it always returns -2001 whether it exists or not.
This should get it done:

Dim txt As String
txt = "text to be found"

Dim txt_array(##) As String
'the array is filled up with desired data.

Dim i As Integer

For i = 0 To txt_array.Length - 1 ' length starts at 1, but indexing starts at 0
If txt = txt_array(i) Then
MsgBox(i)
End If
Next
Reply With Quote
  #4  
Old 07-11-2004, 06:58 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

To use BinarySearch, you have to Sort it first.

Array.Sort(xyz)
Res = Array.BinarySearch(xyz, "h")
something like this.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #5  
Old 07-12-2004, 12:33 PM
davcaw's Avatar
davcaw davcaw is offline
Centurion
 
Join Date: Mar 2004
Location: Cedarburg, WI, USA
Posts: 138
Default

well, i ran in to another problem. The reason it wasn't working was because the string had a "p" on the end that was not supposed to be there. It seems that when i use the remove function, it removes the letters i tell it to, but then it adds a letter "p" to the end
Reply With Quote
  #6  
Old 07-12-2004, 12:49 PM
Iceplug's Avatar
Iceplug Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: California, USA
Posts: 16,583
Default

And what line does this occur on?
It must be something that you are accidentally assigning. You should be able to check by adding a few messagebox.Show statements.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->