Supersoaker Supersoaker - 1 year ago 92 Question

Accesing Object from collection VB

i'm developing a drugstore system as a task in college, it is a three layer architecture and i have a problem accesing data from a collection.

How can i access specific data from a collection?. E.g.:

If _pat.dao.Patients.Item(1).Equals(patientID) Then
idcat = _pat.PatCategory
End If

The collection is full with patient objects each one with its own attributes(ID, name ....)


basically retrieves the whole collection, and i'm trying to find the object where the ID fits patientID to get the category from that object. I don't properly know how the Item property works and i dont' know if the Item(1) is retrievieng the first object of the collection or the first attribute of every object. Any help on this?

Thank you very much.

Answer Source

You could use the FirstOrDefault collection extension method to locate the patient:

Dim patient = _pat.dao.Patients.FirstOrDefault(Function(p) p.ID = patientID)

If the patient is found it will be returned to the patient variable. If it's not found the patient variable will be null, which in VB.NET is Nothing:

If patient IsNot Nothing Then
  idcat = patient.PatCategory
End If
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download