搜索顺序文件加载到数组vb.net并搜索

时间:2014-11-18 14:41:12

标签: arrays vb.net search

我正在使用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


Private Sub btnLoadArray_Click(sender as Object,e As EventArgs)处理btnLoadArray.Click         Dim filePath As String =&#34; library.txt&#34;

    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

2 个答案:

答案 0 :(得分:1)

好吧,我没有使用数组并处理所有复杂问题,我建议您使用更多面向对象的方法

所以开始定义一个类来保存像这样的专辑的信息

Public Class Album
   Public ArtistName as String
   Public AlbumTitle as String
   Public Function ToString() as String
        return ArtistName & " - " & AlbumTitle
   End Function
End Class

现在,您可以使用List(Of Album)删除阵列混乱并要求此列表中的每个项目通过ToString方法呈现其内容。

此处的一个重要作用是保留给IEnumerable function Where,它从List(Of Album)中提取所有尊重Where方法所需的Lambda expression的元素。

Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

    ' Here the list is fixed but you can easily build it loading data from a database
    ' or from some other storage medium like a file etc...
    Dim albumList = new List(Of Album) From _
    { 
        new Album With { .ArtistName = "Dillinja",  .AlbumTitle = "Untitled" },
        new Album With { .ArtistName = "TeeBee",  .AlbumTitle = "Scorpion"},
        new Album With { .ArtistName = "Dieselboy",  .AlbumTitle =  "Horns" },
        new Album With { .ArtistName = "TeeBee",  .AlbumTitle = "Blades" }
    }

    ' To help search you could integrate the ToLower expressions in 
    ' Plutonix answer here...
    Dim searchTerm = txtSearch.Text
    Dim searchResult = albumList.Where(Function(x) x.ArtistName = searchTerm)

    lstLibrary.Items.Clear()
    if searchResult.Count > 0 Then
        For Each item in searchResult
            lstLibrary.Items.Add(item.ToString())
        Next
    Else
       MessageBox.Show("Not found")
    End if 
End Sub

答案 1 :(得分:0)

List(of String)不是数组,而是更容易管理,提供更高的效率和您可能想要的功能。

Private Artists As New List(of String)

....
Artists.Addrange({"Dillinja", "TeeBee", "Dieselboy", "TeeBee"})
...
' find a single item:
If Artists.Contains(txtSearch.Text) Then      ' no looping required
   ' a dupe
Else
    ' not a dupe
End If

获取所有匹配的项目:

Dim find = Artists.Where(Function(s) _ 
    s.ToLowerInvariant = txtSearch.Text.ToLowerInvariant).ToList

结果find也将是包含匹配项的List(of String)。这次它不区分大小写。要获得欺骗的计数

Dim finds = Artists.LongCount(Function(n) n.ToLowerInvariant =  _   
       txtSearch.Text.ToLowerInvariant.ToLowerInvariant)

我不知道有多少价值。既不是2也不是&#34; Teebee&#34;,&#34; TeeBee&#34;是非常有用或有趣的。更典型的是,您希望返回是与搜索词相关联的整个对象(如相册)。这需要一个可以将Arist,Album,Genere,Tracks等粘合在一起的Class(请参阅Steve的答案,开始这一部分)。

对于整个文件部分,列表可以很容易地序列化,允许整个列表以2-3行代码保存或从磁盘重新加载。