1 Replies - 337 Views - Last Post: 14 July 2014 - 03:11 AMRate Topic:

Class Array Search using IndexOf

Posted 14 July 2014 - 12:14 AM

Hi there, I'm using VB.NET (VS 2008) and I have question about searching an array of objects.
All the examples I've seen have been for simple string or numeric arrays when using the IndexOf
search function but I'm having trouble getting it to work for an array of objects. As an example
(see below) I have a person class with three properties FirstName (string), Surname (string), Age(integer)
If I try to retrieve the age of the person call "Joe" by searching for the associated index
using the firstname, I get -1 returned. Is this the correct way to do a search on class arrays
or is there a better method ?

Thanks for any help,
Dave

public class person
Public FirstName as string
Public Surname as string
Public Age as integer
end class
dim people(9) as person
for i = 0 to 9
people(i) = new person
next i
'populate the array ... then do a search
dim idx = array.indexof(people,"Joe")
'idx returns as -1

This post has been edited by andrewsw: 14 July 2014 - 03:02 AM
Reason for edit:: Added CODE tags