title: Visual Basic code works but is inelegant - any suggestions?
body: <p>I am trying to stay ahead of my Year 12 Software class. Starting to work with records and arrays. I have answered the question, but the solution feels very clunky. I am hoping someone has suggestions/links for completing this task in a more efficient way.
  The task: read in lines from a text file and into a structure, and then loop through that, populating four list boxes if an animal hasn't been vaccinated.
  Here's the code:</p>
  <pre><code>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(&quot;patients.txt&quot;)
          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(&quot;,&quot;)
                  '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 = &quot;No&quot; 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 = &quot;The total number of unvaccinated animals is &quot; &amp; CStr(totNotVac)
                  End If
  
              Next
          Catch ex As Exception
              MsgBox(&quot;Something went wrong with the file&quot;)
          End Try
          PetFile.Close()
  
      End Sub
  
      Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
          Close()
  
      End Sub
  End Class
  </code></pre>
  <p>And one line from the patient.txt file:</p>
  <pre><code>    Richard Gere,16 Sunset Blvd,Gerbil,No
  </code></pre>
  <p>I hope this isn't out of place.</p>
  <p>Regards,</p>
  <p>Damian</p>