Avatar of randys106
randys106
Flag for United States of America

asked on 

List of (myClass) troubles

I have a class called Patients with three properties; Patient.Name as string, Patient.ID as integer, Patient.ExternalID as string.  

The following code works fine.  That is, the listbox loads.
   Private Sub frmPatientLookup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.PatientsTableAdapter.Fill(Me.PatientsDataSet.Patients)
        For Each r In PatientsDataSet.Tables(2).Rows
            lstPatients.Items.Add(r(2) & ", " & r(1))
        Next
    End Sub

Open in new window

r(2) is the Last Name, r(1) is the First Name.

But, I am trying to populate a list consisting of the Patents Class.  

This code does not work:
 Private PatientList As List(Of Patients)
 Private Sub frmPatientLookup_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        PatientList = New List(Of Patients)
        Dim aPatient As Patients
        Me.PatientsTableAdapter.Fill(Me.PatientsDataSet.Patients)
        For Each r In PatientsDataSet.Tables(2).Rows
            lstPatients.Items.Add(r(2) & ", " & r(1))
            aPatient.Name = r(2) & ", " & r(1)
            aPatient.ID = r(0)
            aPatient.ExternalID = r(3)
            PatientList.Add(aPatient)
        Next
    End Sub

Open in new window

There are (at least) three problems:
1) I get an warning telling me that "Variable 'aPatient' is used before it has been assigned a value. A null reference exception could result at runtime."
 2) It does not load the list
3) It only loads the first item in the dataset into the listbox.

What am I doing wrong?
Visual Basic.NET

Avatar of undefined
Last Comment
it_saige

8/22/2022 - Mon