Results 1 to 7 of 7

Thread: [RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [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
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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)
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: List(Of T).FindIndex: How to handover an argument in the Predicate(T)

    Quote Originally Posted by opus View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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))
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,024

    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.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    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.

  7. #7

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Re: [RESOLVED] List(Of T).FindIndex: How to handover an argument in the Predicate(T)

    Quote Originally Posted by Evil_Giraffe View Post
    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 ;-)
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width