Visual Basic .NET Forums  
Click here to advertise with us

Go Back   Visual Basic .NET Forums > VB.NET > VB.NET General Discussion

VB.NET General Discussion VB.NET general discussion area

VB.NET Forums Newsletter Signup:
Email address:


Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-04-2010, 10:17 AM
goldengel's Avatar
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jan 2010
Age: 31
Posts: 9
Reputation: 0
goldengel is on a distinguished programming path ahead
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-04-2010, 6:44 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 877
Reputation: 500
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-06-2010, 3:12 AM
goldengel's Avatar
VB.NET Forum Newbie
.NET Framework: .NET 2.0
 
Join Date: Jan 2010
Age: 31
Posts: 9
Reputation: 0
goldengel is on a distinguished programming path ahead
Default

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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-06-2010, 12:15 PM
VB.NET Forum Idol
.NET Framework: .NET 2.0 (VS 2005)
 
Join Date: Feb 2008
Location: USA
Posts: 877
Reputation: 500
MattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond reputeMattP has a reputation beyond repute
Default

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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Bookmarks

Tags
find, findall, predicate


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





All times are GMT -4. The time now is 11:07 PM.

Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2


For advertising opportunities click here.