We help IT Professionals succeed at work.
Get Started

List of (myClass) troubles

randys106
randys106 asked
on
123 Views
Last Modified: 2016-07-10
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?
Comment
Watch Question
"Batchelor", Developer and EE Topic Advisor
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 3 Answers and 10 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE