user5962847 user5962847 - 1 month ago 8
Vb.net Question

Search delimited textfile in 2D array

I have delimited a text file and put it into a 2D array. Then, I have tried to find out if the array contains the product ID (entered in a textbox). However, the code I have used to try to search the array and show the name does not work.
The textfile says:
1, Frances
2, Emma

Here is my code:

Public Class Form1
Dim filename As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
filename = "names.txt"

End Sub

Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click


filename = "names.txt"
FileOpen(1, filename, OpenMode.Input,,,)

Dim lines() As String = IO.File.ReadAllLines("names.txt")
Dim values(lines.Length - 1, 1) As String
For i As Integer = 0 To lines.Length - 1
Dim parts() As String = lines(i).Split(","c)
values(i, 0) = parts(0)
values(i, 1) = parts(1)
Next

Dim ID As String
ID = txtidsearch.Text

Dim line As String

Do While Not EOF(1)
line = LineInput(1)

If values().Contains(ID) Then
lblid.Text = line
Application.DoEvents()
GoTo line1
ElseIf EOF(1) = True
MsgBox("Not Found")

End If
Loop
line1:
FileClose(1)

End Sub
End Class


Thanks in advance

Answer

What I would do is create classes to hold your people. It would make it easier in the long run. I keep these kind of classes in one VB file however it's up to you. You could split Person and People into separate files. So here are the classes:

Imports System.Collections.ObjectModel
Public Class Person
   Public Key As String

   Public Sub New(ByVal id As Integer,
                  ByVal name As String,
                  ByVal form as String)

       _id = id
       _name = name
       _form = form

   End Sub

   Private _id As Integer
   Public ReadOnly Property Id() As Integer
       Get
           Return _id
       End Get

   End Property

   Private _name As String
   Public ReadOnly Property Name() As String
       Get
           Return _name
       End Get
   End Property

   Private _form As String
   Public ReadOnly Property Form() As String
       Get
           Return _form
       End Get
   End Property

   End Class

Public Class People
    Inherits KeyedCollection(Of String, Person)

    Protected Overrides Function GetKeyForItem(ByVal item As Person) As String
        Return item.Key
    End Function

End Class

I would then change this bit of code:

    Dim values(lines.Length - 1, 1) As String
    For i As Integer = 0 To lines.Length - 1
        Dim parts() As String = lines(i).Split(","c)
        values(i, 0) = parts(0)
        values(i, 1) = parts(1)
    Next

To this:

    Dim myPeople As New People
    For i As Integer = 0 To lines.Length - 1
        Dim parts() As String = lines(i).Split(","c)
        myPeople.Add(New Person(parts(0), parts(1), parts(2)))
    Next

And I would replace this code:

    Dim line As String

    Do While Not EOF(1)
        line = LineInput(1)

        If values().Contains(ID) Then
            lblid.Text = line
            Application.DoEvents()
            GoTo line1
        ElseIf EOF(1) = True
            MsgBox("Not Found")

        End If
    Loop
line1:

With:

    Dim filteredLines = From people In myPeople
                        Where people.Id = ID
                        Select people

    If filteredLines.Count = 1 Then
        Label1.Text = filteredLines(0).Name
    End If