Results 1 to 4 of 4

Thread: How to work with predicate and .Find methode

  1. #1
    goldengel's Avatar
    goldengel is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    9
    Reputation
    0

    Question How to work with predicate and .Find methode

    Hi.

    I have no chance to simple search a property in my property list by using the ".Title" of the property. Instead of using "For each > If .Title = SearchTitle" I prefer to do it with the Predicate and ".Find" methode.
    Can someone help me to find out, how to avoid using the local variable " _SearchProperty" in the example below?


    Code:
          Private Sub NumericUpDownFrequency_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDownFrequency.ValueChanged
            Dim PropFrequency As F_SCANT30_DEVICES.TB.Devices.clsFscanProperty
            _SearchProperty = "Freqency"
            PropFrequency = Me.cRemote.FscanProperties.Find(AddressOf CompareProperty)
        End Sub
    
        Private Function CompareProperty(ByVal Prop As F_SCANT30_DEVICES.TB.Devices.clsFscanProperty) As Boolean
            If Prop.Title = Me._SearchProperty Then
                Return True
            Else
                Return False
            End If
        End Function

  2. #2
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,207
    Reputation
    559
    Passing parameters to delegates in VB.NET

    Wrapper class for a Predicate(Of T)

    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
    Generic user class I used for testing.

    Code:
    Public Class User
    
    	Public Sub New(ByVal firstName As String, ByVal lastName As String)
    		Me._firstName = firstName
    		Me._lastName = lastName
    	End Sub
    
    	Private _firstName As String
    	Public Property firstName() As String
    		Get
    			Return _firstName
    		End Get
    		Set(ByVal value As String)
    			_firstName = value
    		End Set
    	End Property
    
    
    	Private _lastName As String
    	Public Property lastName() As String
    		Get
    			Return _lastName
    		End Get
    		Set(ByVal value As String)
    			_lastName = value
    		End Set
    	End Property
    
    End Class
    Example usage where I pass a search parameter 'Jim' in for the first name.

    Code:
    Public Class Form1
    
    	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    		Dim userList As New List(Of User) _
    		 (New User() {New User("Jim", "Smith"), New User("John", "Doe"), New User("Bill", "Jones")})
    		Dim searchedUser As User = _
    		 userList.Find(New PredicateWrapper(Of User, String)("Jim", AddressOf FirstNameMatch))
    	End Sub
    
    	Private Function FirstNameMatch(ByVal item As User, ByVal searchArg As String) As Boolean
    		Return item.firstName.Equals(searchArg)
    	End Function
    
    End Class

  3. #3
    goldengel's Avatar
    goldengel is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Jan 2010
    Posts
    9
    Reputation
    0
    Hi MattP

    Thank you a lot for taking your time to write this example. The idea of using a
    Code:
    Shared winding operator
    is very new for me. So, I never could not fix the problem without help.

    I have two questions about your answer:
    1. In my declaration of the wrapper class, what could I write in the description of parameters T and A?
    Code:
                ''' <summary>
                ''' Wrapper class to simplify the List.Find methode with any keyword argument.
                ''' </summary>
                ''' <typeparam name="T">   </typeparam>
                ''' <typeparam name="A">   </typeparam>
    2. I am interested in where you got this informations from. I had a book. But because of never using some things, I think I over-read it. Perhaps you can give me an input where I can search for any answer next time?


    Regards
    Timo

  4. #4
    MattP is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 4.0
    Join Date
    Feb 2008
    Location
    WY, USA
    Posts
    1,207
    Reputation
    559
    Goldengel, not a problem at all. I can tell you put in the work to get a solution going so I have no problem spending the time to help out.

    Quote Originally Posted by goldengel View Post
    1. In my declaration of the wrapper class, what could I write in the description of parameters T and A?
    Code:
                ''' <summary>
                ''' Wrapper class to simplify the List.Find methode with any keyword argument.
                ''' </summary>
                ''' <typeparam name="T">   </typeparam>
                ''' <typeparam name="A">   </typeparam>
    Have a look at <typeparam> (C# Programming Guide). The article is for C# but is just as applicable for VB.NET.

    Quote Originally Posted by goldengel View Post
    2. I am interested in where you got this informations from.
    I linked the article where I found the PredicateWrapper class in my original response. If you're interested in learning more about Widening and Narrowing I'd suggest this msdn article as a starting point: Widening and Narrowing Conversions

    You should note that both of the links I provided are from the msdn site which is where I usually start.

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Optimization by vBSEO