SO how do I do this?

mojoman

Member
Joined
Nov 21, 2006
Messages
20
Programming Experience
Beginner
I have inputted all information in a text file into a list:

Try
sourceFile = "\My Documents\TOPR1.TXT"
Dim objReader AsNew IO.StreamReader(sourcefile)
'Peek returns a value of -1 when there are no more...
'...incoming characters thereby signifying EOF
'Add all items in TOPR1.TXT to a list
While objReader.Peek <> -1
infoList.Add(objReader.ReadLine.Trim())
EndWhile
objReader.Close()
Catch ex As Exception
txtWo.Text = "I/O ERROR"
EndTry

----------
What I need now to do is to search the whole list for an item scanned by a handheld scanner which is in e.text. Please note that each line item in the list contains many strings, some of them separated by spaces and commas, and string I am searching for is a SUBSTRING of one of the STRINGS in a line item.

Will this line do the job?

IfNot (infoList.Contains(e.Text)) Then
txtWo.Text = "NO ENTRY"
EndIf
 
In this case yes, it would appear that you are loading the entire line into the List. In this case, it should be stored as a string.

So, when you scan something with your handheld, you want to check each string in the List to see if it contains the data from the scan (at least thats what I'm understanding). So, you do the:

if SearchString.Contains(e.Text)
 
This is a modified version of the code I posted you earlier. Contains should work fine, as it did for me just now:

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myList [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf1"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf2"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf3"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf4"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf5"[/COLOR][/SIZE][SIZE=2])[/SIZE]
 
[SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] searchString [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] myList[/SIZE]
[SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] searchString.Contains([/SIZE][SIZE=2][COLOR=#800000]"asdf3"[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]  MessageBox.Show([/SIZE][SIZE=2][COLOR=#800000]"Match found!"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff] End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE]
 
mojoman, I suggest you change you user profile to reflect your .Net platform, it currently says you're using .Net 2.0. (where String.Contains is valid)
 
Direct copy paste from these forums is a bit trick on those two. For some reason the spaces are lost.

They should be "For Each" and"As String In"

The spaces are lost because of the attempt at code highlighting implemented by the board software.
To post code properly you must turn this off. Press the
switchmode.gif
button in the TOP RIGHT of the reply screen (not quick reply) if you wish to post code

THis will set the forum reply box into BASIC MODE temporarily. Its no longer WYSIWYG, and you see the code tags
 
I use vb.net 1.1, and i use the following code to find a string in another:

VB.NET:
If Not StringToCheck.IndexOf(SmallerString) < 0 Then
'The SmallerString Is Found
...
End If


Just replace this for the If ... Compare Lines from prev post.

You could use an ArrayList to store your lines, and possibly using IComparer to search the items of the array would be faster but with more code. I can't remember how to use the IComparer interface but if you google it, you should find good examples to work from.


Another way would be to use DataSet/DataTable to store the lines, you could also save the file as an XML file for faster loading. Then the code to find the line would be:

VB.NET:
Dim MyFoundRow As DataRow = MyDataTable.Select("Str1 LIKE '*" & SmallerString & "*'")(0)

Hope this helps
 
Back
Top