Arrays Visual Basic代码可以工作,但不雅观-有什么建议吗?

Arrays Visual Basic代码可以工作,但不雅观-有什么建议吗?,arrays,vb.net,forms,visual-studio,Arrays,Vb.net,Forms,Visual Studio,我正努力在12年级的软件课上保持领先。开始使用记录和数组。我已经回答了这个问题,但这个解决方案感觉很笨拙。我希望有人能提供建议/链接,以更有效的方式完成这项任务。 任务:将文本文件中的行读入一个结构,然后循环阅读,如果动物没有接种疫苗,则填充四个列表框。 代码如下: Imports System.IO Public Class Form1 'Set up the variables - customer record, total pets not vaccinated, total r

我正努力在12年级的软件课上保持领先。开始使用记录和数组。我已经回答了这个问题,但这个解决方案感觉很笨拙。我希望有人能提供建议/链接,以更有效的方式完成这项任务。 任务:将文本文件中的行读入一个结构,然后循环阅读,如果动物没有接种疫苗,则填充四个列表框。 代码如下:

Imports System.IO
Public Class Form1
    'Set up the variables - customer record, total pets not vaccinated, total records in the file, and a streamreader for the file.
    Structure PetsRecord
        Dim custName As String
        Dim address As String
        Dim petType As String
        Dim vacced As String
    End Structure
    Dim totNotVac As Integer
    Dim totalRecCount As Integer
    Dim PetFile As IO.StreamReader

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click

        'set an array of records to store each record as it comes in. Limitation: you need to know how many records in the file. Set the array at 15 to allow for adding more in later.
        Dim PetArray(15) As PetsRecord
        'variables that let me read in a line and split it into sections.
        Dim lineoftext As String
        Dim i As Integer
        Dim arytextfile() As String
        'tell them what text file to read
        PetFile = New IO.StreamReader("patients.txt")
        totNotVac = 0

        Try
            totalRecCount = 0
            ' read each line in and split the lines into fields for the records. Then assign the fields from the array. Finally, reset the array and loop.
            Do Until PetFile.Peek = -1
                'read in a line of text 
                lineoftext = PetFile.ReadLine()
                'split that line into bits separated by commas. these will go into the array.
                arytextfile = lineoftext.Split(",")
                'dunno whether this is the best way to do it, but stick the array bits into the record, and then clear the array to start again.
                PetArray(totalRecCount).custName = arytextfile(0)
                PetArray(totalRecCount).address = arytextfile(1)
                PetArray(totalRecCount).petType = arytextfile(2)
                PetArray(totalRecCount).vacced = arytextfile(3)

                totalRecCount += 1
                Array.Clear(arytextfile, 0, arytextfile.Length)
            Loop

            For i = 0 To PetArray.GetUpperBound(0)

                If PetArray(i).vacced = "No" Then
                    lstVaccinated.Items.Add(PetArray(i).vacced)
                    lstCustomer.Items.Add(PetArray(i).custName)
                    lstAddress.Items.Add(PetArray(i).address)
                    lstPetType.Items.Add(PetArray(i).petType)

                    totNotVac += 1
                    lblVacTotal.Text = "The total number of unvaccinated animals is " & CStr(totNotVac)
                End If

            Next
        Catch ex As Exception
            MsgBox("Something went wrong with the file")
        End Try
        PetFile.Close()

    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Close()

    End Sub
End Class
和patient.txt文件中的一行:

    Richard Gere,16 Sunset Blvd,Gerbil,No
我希望这不是不合适的地方

问候,


Damian

如果不需要“PetsRecord”数组来存储数据,请查看以下代码:

Dim totNotVac As Integer
Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    File.ReadAllLines("your text file path").ToList().ForEach(Sub(x)
                                                          lstVaccinated.Items.Add(x.Split(","c)(0))
                                                          lstCustomer.Items.Add(x.Split(","c)(1))
                                                          lstAddress.Items.Add(x.Split(","c)(2))
                                                          lstPetType.Items.Add(x.Split(","c)(3))
                                                          totNotVac += 1
                                                      End Sub)
    lblVacTotal.Text = "The total number of unvaccinated animals is " & CStr(totNotVac)
End Sub

如果您想使用流,请注意它们需要被处理

File.ReadAllLines
返回一个行数组。由于数组是在声明的位置初始化的,因此不需要指定大小

如果使用
List(Of T)
则不必事先知道列表中的元素数量。避免了数组的限制

为每个使用
,可以避免使用大小。
后面的小
c
,“
告诉编译器这是一个
Char
,这是
.Split
所期望的。如果您有一个选项Strict On(您总是应该这样做),您就会看到一个错误。您可以通过创建
新的PetsRecord
将项目添加到列表中。参数化的
子新建
接收值并设置属性

不要在每次迭代时更改标签中的显示。使用插值字符串(前面有一个
$
),该字符串允许嵌入的变量被大括号
{}
包围。无需将数字更改为字符串,因为它是由插值器暗示的。(在VS2015及更高版本中提供)


这不是这个网站的目的。有一些是为了代码审查。我投票结束这个问题,因为没有实际问题要解决。不雅是旁观者眼中的一个词,对一个人来说优雅的东西对另一个人来说可能不是。我确实有一个你可以考虑的评论,为什么在数组中通过拆分作为DO循环中的第二行代码重新分配时,在AythTreFror文件中调用“Realth.Office”?这似乎是一个不必要的步骤。非常优雅和简洁,但没有解释,对一个明显的初学者没有太大帮助。
Public Structure PetsRecord
    Public Property custName As String
    Public Property address As String
    Public Property petType As String
    Public Property vacced As String
    Public Sub New(name As String, add As String, pType As String, vac As String)
        custName = name
        address = add
        petType = pType
        vacced = vac
    End Sub
End Structure

Private Sub btnLoad_Click(sender As Object, e As EventArgs) Handles btnLoad.Click
    Dim lines = File.ReadAllLines("patients.txt")
    Dim lstPet As New List(Of PetsRecord)
    For Each line In lines
        Dim splits = line.Split(","c)
        lstPet.Add(New PetsRecord(splits(0), splits(1), splits(2), splits(3)))
    Next
    Dim totNotVac As Integer
    For Each pet In lstPet
        If pet.vacced = "No" Then
            lstVaccinated.Items.Add(pet.vacced)
            lstCustomer.Items.Add(pet.custName)
            lstAddress.Items.Add(pet.address)
            lstPetType.Items.Add(pet.petType)
            totNotVac += 1
        End If
    Next
    lblVacTotal.Text = $"The total number of unvaccinated animals is {totNotVac}"
End Sub