Return the index of a child in collection

Return the index of a child in collection

Old forum URL:

TScorpio posted on Wednesday, July 27, 2011

I need to search a child collection based on a property value and return the childs index number.

I'm currently using this:

Public Function FindChildIndex(ByVal childGuid As Guid) As Integer
 For i As Integer = 0 To Me.Count - 1
  If Me.Item(i).ToString.Equals(childGuid.ToString) Then
   Return i
  End If
 Return -1
End Function

This doesn't work so well when the collection gets large...

I'm looking for a better way to implement this, any suggestions?




JonnyBee replied on Wednesday, July 27, 2011

How large can your collection get?

You should not use the ToString method - try rather the IComparable.CompareTo method.
That may speed thing up a little.

  If childGuid.CompareTo(Me.Item(i)) = 0 Then

With really large collections you could look into using some indexing library, f.ex and I'm sure there are others as well.




TScorpio replied on Tuesday, August 02, 2011

750,000 - 1,000,000 records. It's not typical, but they do exist out there.

I've glanced at the indexing library, it looks helpful. Thanks!

Copyright (c) Marimer LLC