VB.NET 2010을 사용 중이며 순차 파일을 사용하여 검색 기능을 구현하려고합니다. 원래 순차 파일을 사용하여 읽기 및 검색을 시도했지만 극히 어려웠으므로 데이터가있는 두 개의 다른 배열을 사용하고 아티스트 및 앨범을 검색하려고 간단한 접근 방식을 취했습니다.순차 파일로드 배열 검색 및 검색
아래 코드는 아티스트 검색을위한 txtSearch.Text의 입력을 사용합니다. 필자는 중복 된 아티스트 "TeeBee"를 가지고 있지만 그 아티스트를 검색 할 때 아티스트 "TeeBee"아래에 두 개의 다른 앨범이 있으므로 두 개가 아닌 하나의 결과 만 수신합니다.
다른 루프를 추가하려고 생각했지만 작동하지 않습니다. 결과에 대한 수익을 계속할 수있는 방법이 없기 때문에 결과가 잘려나 가고 있다고 생각했습니다.
저는 초보자이므로 프로그래밍을 염두에 두시기 바랍니다.
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
' Artist array
Dim strArtist() As String = {"Dillinja", "TeeBee", "Dieselboy", "TeeBee"}
' Album array
Dim strAlbum() As String = {"Untitled", "Scorpion", "Horns", "Blades"}
Dim strSearchForArtist As String
Dim intSub As Integer
' artist search
strSearchForArtist = txtSearch.Text
Do Until intSub = strAlbum.Length OrElse
strSearchForArtist = strArtist(intSub)
intSub = intSub + 1
Loop
If intSub < strArtist.Length Then
lstLibrary.Items.Add(strArtist(intSub) & " " & strAlbum(intSub) & vbNewLine)
Else
MessageBox.Show("Invalid", "Bad", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
배열에 텍스트 파일을로드하려고하지만, 앨범, 아티스트 장르와 같은 별도의 배열을 만들지 않습니다. - 11-19-14
개인 서브 btnLoadArray_Click (개체로 보낸 사람, EventArgs입니다으로 전자) 문자열 = "library.txt"배열보다는
Dim sr As IO.StreamReader
sr = IO.File.OpenText(filePath)
' look inside file and read every line
' this will be how we put the number for
' our array below
Dim TotalLines As Integer = 0
Dim word As String = ""
' need 3 to be dynamic so we get all lines in the file to build the array words(#)
' wanted to use words(,) but that does not work
Dim words() As String = IO.File.ReadAllLines(filePath)
Dim i As Integer = 0
' when the peak value is -1 we're at the end of the file
Do Until sr.Peek = -1
' load one at a time
word = sr.ReadLine()
' load word into array
words(i) = word
' output
lstArtist.Items.Add(words(i))
' increment counter
i += 1
Loop
'close the file
sr.Close()
End Sub
왜 클래스를 사용하여 별도의 배열 대신 커플 아티스트/앨범을 나타내지 않습니까? – Steve