VB.net 将 .txt 文件放入 Class 列表并将 属性 分配给 .txt 文件的某些索引
VB.net Placing a .txt file into a Class List and assigning a property to certain indexes of the .txt file
我有一个问题,我已经为我的排序程序创建了一个 class 列表,所以我可以根据名称对项目进行排序。我能够使用分配的数据来做到这一点,但我不知道如何从 txt 文件向 class 添加数据,也不知道如何在文件加载后为这些项目分配属性。
我当前的代码:
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
txt文件中的一些数据,第一行是姓名,第二行是年龄,第三行是身高,第四行是体重:
蒙蒂雷耶斯
28
1700
70.7
基尔伯克
45
1800
93.5
我的目标是使用基于名称的冒泡排序对 txt 文件中的数据进行排序。没有数据,真的无法做到这一点。希望外面有人可以帮助我或给我一些线索。
使用 ReadAllLines
而不是 ReadAllText
将为您提供文件中所有行项目的数组。然后您可以遍历该数组并逐行提取数据以创建和填充您的 Patient 对象,然后将其插入到您的列表中。
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
我有一个问题,我已经为我的排序程序创建了一个 class 列表,所以我可以根据名称对项目进行排序。我能够使用分配的数据来做到这一点,但我不知道如何从 txt 文件向 class 添加数据,也不知道如何在文件加载后为这些项目分配属性。
我当前的代码:
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
txt文件中的一些数据,第一行是姓名,第二行是年龄,第三行是身高,第四行是体重:
蒙蒂雷耶斯
28
1700
70.7
基尔伯克
45
1800
93.5
我的目标是使用基于名称的冒泡排序对 txt 文件中的数据进行排序。没有数据,真的无法做到这一点。希望外面有人可以帮助我或给我一些线索。
使用 ReadAllLines
而不是 ReadAllText
将为您提供文件中所有行项目的数组。然后您可以遍历该数组并逐行提取数据以创建和填充您的 Patient 对象,然后将其插入到您的列表中。
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