How to work with predicate and .Find methode

goldengel

Member
Joined
Jan 4, 2010
Messages
9
Programming Experience
3-5
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?


VB.NET:
      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
 
Passing parameters to delegates in VB.NET

Wrapper class for a Predicate(Of T)

VB.NET:
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.

VB.NET:
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.

VB.NET:
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
 
Hi MattP

Thank you a lot for taking your time to write this example. The idea of using a
VB.NET:
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?
VB.NET:
            ''' <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
 
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.

1. In my declaration of the wrapper class, what could I write in the description of parameters T and A?
VB.NET:
            ''' <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.

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.
 

Latest posts

Back
Top