Home > Mobile >  VB.net Placing a .txt file into a Class List and assigning a property to certain indexes of the .txt
VB.net Placing a .txt file into a Class List and assigning a property to certain indexes of the .txt

Time:02-01

I have a problem, I've created a class list for my sort program, so I can sort the items based on names. I was able to do it with assigned data but I don't know how to add data to the class from a txt file, nor assigning properties to those items once the file is loaded.

My current code:

Public Class PatientSorter
    Public Class Patients
        Property Name As String
        Property Age As Integer
        Property Weight As Double
        Property Height As Integer

        Public Overrides Function ToString() As String
            Return String.Format("{0}: {1} years old, {2} mm, {3} kg", Name, Age, Height, Weight)
        End Function

End Class


Public Sub PatientSorter_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim i As Integer

    Do While i <= DataEntry.lstPatientArray.Items.Count - 1
        lstCurrentData.Items.Add(DataEntry.lstPatientArray.Items(i))
        i = i   1
    Loop


End Sub

Private Sub btnSearchForm_Click(sender As Object, e As EventArgs) Handles btnSearchForm.Click

    Me.Hide()
    Search.Show()

End Sub

Public Sub BubbleSort(ByRef patients As List(Of Patients))

        For i = 0 To patients.Count - 2
            Dim doneSwap = False
            For j = i   1 To patients.Count - 1
                If patients(i).Name > patients(j).Name Then
                    Dim tmp = patients(j)
                    patients(j) = patients(i)
                    patients(i) = tmp
                    doneSwap = True
                End If
            Next
            If Not doneSwap Then Return
        Next

    End Sub

    Public Sub btnNameSort_Click(sender As Object, e As EventArgs) Handles btnNameSort.Click

        Dim fileReader As String
        fileReader = My.Computer.FileSystem.ReadAllText("C:\Data.txt")
        Dim patients As New List(Of Patients)


        BubbleSort(patients)

    End Sub
End Class

Some of the Data in the txt file, first line is name, second is age, third is height and fourth is weight:

Monty Reyes

28

1700

70.7

Kier Burke

45

1800

93.5

My goal is to sort the data from the txt file using my bubblesort based on names. Not really able to do that without the data. Hopefully someone out there can help me or give me a clue on something I'm missing.

CodePudding user response:

Using ReadAllLines instead of ReadAllText will give you an array of all line items in the file. You can then loop through that array an extract the data line by line to create and populate your Patient objects, which you'd then insert into your list.

Dim patients As New List(Of Patient)
Dim data = System.IO.File.ReadAllLines("C:\Data.txt") 'read lines into an array

'loop through the array, incrementing the index by 4 each iteration
For index = 0 To data.Length - 1 Step 4
    Dim patient = New Patient() 'create a Patient

    'Populate the patient data by accessing the current 4 array indexes
    patient.Name = data(index)
    patient.Age = data(index   1)
    patient.Weight = data(index   2)
    patient.Height = data(index   3)

    patients.Add(patient) 'add the Patient to the list of Patients 
Next
  •  Tags:  
  • Related