Skip to content

Instantly share code, notes, and snippets.

@wolf99
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wolf99/58ea530ae5d33111c757 to your computer and use it in GitHub Desktop.
Save wolf99/58ea530ae5d33111c757 to your computer and use it in GitHub Desktop.
BetterBindingList(T) class that adds FindIndex method to inherited BindingList(T) class
Imports System.ComponentModel
Imports System.Diagnostics.Contracts
' The FindIndex methods use code converted from the MSDN C# reference source as of 2015 Aug 17 13:34 GMT at:
' http://referencesource.microsoft.com/#mscorlib/system/collections/generic/list.cs,cadc26eb4ba974e4
Public Class BetterBindingList(Of T)
Inherits BindingList(Of T)
Public Function FindIndex(match As Predicate(Of T)) As Integer
Contract.Ensures(Contract.Result(Of Integer)() >= -1)
Contract.Ensures(Contract.Result(Of Integer)() < MyBase.Count)
Return FindIndex(0, MyBase.Count, match)
End Function
Public Function FindIndex(startIndex As Integer, count As Integer, match As Predicate(Of T)) As Integer
If CUInt(startIndex) > CUInt(count) Then
Throw New ArgumentOutOfRangeException("startIndex", New IndexOutOfRangeException())
End If
If count < 0 OrElse startIndex > (MyBase.Count - count) Then
Throw New ArgumentOutOfRangeException("count", New IndexOutOfRangeException())
End If
If match Is Nothing Then
Throw New ArgumentNullException("match", New ArgumentNullException())
End If
Contract.Ensures(Contract.Result(Of Integer)() >= -1)
Contract.Ensures(Contract.Result(Of Integer)() < (startIndex + count))
Contract.EndContractBlock()
Dim endIndex As Integer = startIndex + count
For i As Integer = startIndex To endIndex
If match(MyBase.Items(i)) Then
Return i
End If
Next
Return -1
End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment