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
 
Assuming infoList is a List(of String) you would need to iterate through each item you added to it.

Pseudo:
VB.NET:
blnValueFound=False
For Each string in infoList
  if string = InputFromScanner then
    blnValueFound=True
    Exit For
  endif
Next

if blnValueFound = True then
DoWhateverYouDoWhenYouHaveFoundAMatch
else
DoWhateverYouDoWhenNoMatch
endif
 
Last edited:
Okay this makes sense but each line item in the variable of type list will look like this:

XXX XXXX XXXX XXXXXXXXXXXX

The scanned item will be in the last three positions of the last string of X's. Will your code still work?
 
Yes, but, you can make it faster if your match can only be the last three charactors.

When reading the line into your list, just insert a substring:

infoList.Add(objReader.ReadLine.SubString(ReadLine.Length-3).Trim())

(that code should be right, done w/o the IDE so forgive typos)
 
I tried entering the code you gave but VS 2005 reports errors....it does not like "string". Can you please provide workable code or at least give me the function(s) to check the strings in a line item
 
I don't have your setup, so this isn't exact, but its got the spirit:

VB.NET:
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Form1_Load([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Load
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] myList [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] List([/SIZE][SIZE=2][COLOR=#0000ff]Of[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
 myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf"[/COLOR][/SIZE][SIZE=2])
 myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf1"[/COLOR][/SIZE][SIZE=2])
 myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf2"[/COLOR][/SIZE][SIZE=2])
 myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf3"[/COLOR][/SIZE][SIZE=2])
 myList.Add([/SIZE][SIZE=2][COLOR=#800000]"asdf4"[/COLOR][/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] [/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] searchString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] myList
[/SIZE][SIZE=2][COLOR=#0000ff] If[/COLOR][/SIZE][SIZE=2] searchString = [/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] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff] End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]
 
In VS Studio 2005 it does not recognize ForEach and it does not recognize AsStringIn. WHat class(es) should I import?
 
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"
 
OKay now I have this problem...

Okay, that code works but now I have to do the following: I need to input the entire LINE in which that string is found and perform the following. I to read info from that line from one fixed position to another. How can I do this?
 
dim thesubstring as string = thestring.Substring(index,length)
or just .Substring(index) to return text from index to the end of the string
 
Yeah but

John,

I am not sure I follow what is going on. Is every line item in the list variable considered to be a STRING? If so, your code makes sense to me....
 
Remember JOhn

Every one of my line items in the list variable is not composed of one component but many, some of them are separated by spaces. Hence that is why I asked you is the searchstring in the below one line item:

For Each searchString As String In infoList
If searchString = e.Text Then
Exit Sub
End If

Next
-----------------


 
Since searchString will be the FULL string you loaded into infoList, and e.Text is only a small potential portion of that string (right?) you will need to:

if SearchString.Contains(e.Text) Then

That, or you could chose to only load the portion of the input line that can be matched, but it sounds like you later want to shoot the whole matching input line back out, so that would be a bad choice.
 
okay...

I think we might be getting confuzed by semantincs.

I assume a list variable will just have line after line of code much like the original text file. So when you say the "entire string is loaded" you mean the entire line right? If that is the case, I think I am ok...please clarify.
 
Back
Top