Friday, May 6, 2011

Generic List Equivalent of DataTable.Rows.Find using VB.NET?

I am converting DataTables to a generic list and need a quick and easy way to implement a Find function. It seems I am going to have to use a Predicate. Upon further investigation, I still can't seem to re-create the functionality. I have this predicate...

Private Function ByKey(ByVal Instance As MyClass) As Boolean
    Return Instance.Key = "I NEED THIS COMPARISON TO BE DYNAMIC!"
End Function

And then calling it like this...

Dim Blah As MyClass = MyList.Find(AddressOf ByKey)

But I have no way to pass in a key variable to this predicate to do the comparison, as I used to do with DataTable...

Dim MyRow as DataRow = MyTable.Rows.Find(KeyVariable)

How can I setup a predicate delegate function in VB.NET to accomplish this?

Do not recommend LINQ or lambdas because this is question is regarding .NET version 2.0.

From stackoverflow
  • Just put your predicate in a class instance:

    Public Class KeyMatcher
        Public Sub New(ByVal KeyToMatch As String)
           Me.KeyToMatch = KeyToMatch
        End Sub
    
        Private KeyToMatch As String
    
        Public Function Predicate(ByVal Instance As MyClass) As Boolean
           Return Instance.Key = KeyToMatch
        End Function
    End Class
    

    and then:

    Dim Blah As MyClass = MyList.Find(AddressOf New KeyMatcher("testKey").Predicate)
    

    We can even get a little fancy and make this generic:

    Public Interface IKeyed(Of KeyType)
        Public Key As KeyType
    End Interface
    
    Public Class KeyMatcher(Of KeyType)
        Public Sub New(ByVal KeyToMatch As KeyType)
           Me.KeyToMatch = KeyToMatch
        End Sub
    
        Private KeyToMatch As KeyType
    
        Public Function Predicate(ByVal Instance As IKeyed(Of KeyType)) As Boolean
           Return Instance.Key = KeyToMatch
        End Function
    End Class
    

    And then make your MyClass type implement the new IKeyed interface

    Josh Stodola : That looks like it will work. Thanks! I will do some testing and get back to ya.
    Josh Stodola : Thanks again! I ended up writing a generic class that also uses reflection so that it can be used to match any field to any value from any type. It is certainly not as efficient as without reflection, but I needed to flexibility.
  • Thank you great example vb.net list object find. The first example is working for 100%. But then I try make more generic class (your second example) I have a problems with interface defenition.

    Than I try add 'Public Key As KeyType' in interface defenition VS2008 say me what 'Interface members must be methods, properties, events, or type definitions.' In this case the code is not working. I was try add sub or function to interface but expect troubles to use interface in class.

    Could you please help me resolve this problem.

    Thank you for help and your time in advance.

    Regards, Andrejs.

0 comments:

Post a Comment