[RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Hi,
I'm using a List(of T) and need to geht the Index of one of the elements.
The Search Predicate as shown on MSDN should look like that:
Code:
Private Function FindID(ByVal bk As Book) As Boolean
If bk.ID = IDToFind Then
Return True
Else
Return False
End If
End Function
Isn't it possible to handover the IDToFind as a parameter like:
Code:
Private Function FindID(ByVal bk As Book, ByVal IDToFind as Integer) As Boolean
Re: List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Since books is ListOf(Book), you can check the ID by passing IDToFind in the predicate.
Given a Book object has properties such as ID, Title, Author.
Code:
Dim IDToFind As Integer = 233344
Dim index As Integer = books.FindIndex(Function(t) t.ID = IDToFind)
Re: List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Quote:
Originally Posted by
opus
Isn't it possible to handover the IDToFind as a parameter like:
Code:
Private Function FindID(ByVal bk As Book, ByVal IDToFind as Integer) As Boolean
No you can't. Predicate(Of T) is declared to take only a single parameter not two. You can take advantage of closures though and do what KG did with a lambda.
Re: List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Thanks.
That's what I come up with:
Code:
Public Delegate Function PredicateWrapperDelegate(Of T, A) _
(ByVal item As T, ByVal argument As A) As Boolean
Public Class PredicateWrapper(Of T, A)
Private _argument As A
Private _wrapperDelegate As PredicateWrapperDelegate(Of T, A)
Public Sub New(ByVal argument As A, _
ByVal wrapperDelegate As PredicateWrapperDelegate(Of T, A))
_argument = argument
_wrapperDelegate = wrapperDelegate
End Sub
Private Function InnerPredicate(ByVal item As T) As Boolean
Return _wrapperDelegate(item, _argument)
End Function
Public Shared Widening Operator CType( _
ByVal wrapper As PredicateWrapper(Of T, A)) _
As Predicate(Of T)
Return New Predicate(Of T)(AddressOf wrapper.InnerPredicate)
End Operator
End Class
Usage:
SystemTack is an Object, SystemTracks a Collection of SystemTrack
Code:
'The SearchFunction
Public Shared Function FindIndexTrackNumber(ByVal SysTrack As SystemTrack, ByVal searchArg As Integer) As Boolean
'Searches for the SystemTrack with TrackNumber searchArg
Return SysTrack.TrackNumber.Equals(searchArg)
End Function
'somewhere in code
Dim Index As Integer = SystemTracks.FindIndex(New PredicateWrapper(Of SystemTrack, Integer)(SearchNumber, AddressOf SystemTrack.FindIndexTrackNumber))
Re: [RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Your custom predicate wrapper works! I just tested this with a simple List object. :) :thumb:
Re: [RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Um, what was wrong with a simple lambda?
Code:
Dim Index As Integer = SystemTracks.FindIndex(Function (track) track.TrackNumber.Equals(searchArg))
Or if you want to use it repeatedly, why not return a predicate from a function? (Again, this uses closures)
Code:
Public Function FindTrackNumber(trackNumber As Integer) As Predicate(Of SystemTrack)
Return Function(track) track.TrackNumber.Equals(trackNumber)
End Function
' ...
SystemTracks.FindIndex(FindTrackNumber(searchArg))
Both of these are far clearer than the PredicateWrapper; I especially liked your use of a widening cast to supply a property of it to the FindIndex function, without having to type the property name.
Re: [RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)
Quote:
Originally Posted by
Evil_Giraffe
Um, what was wrong with a simple lambda?
Quick and easy: Me.Stupid.
Nothing is wrong with that, I was just getting it a bit overcomplicated, maybe because my head was also injured lately ;-)