| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
Custom Predicate
Just wanted to add to this post a simple custom predicate class i wrote, to test this out. this will search a list of type string for a specific value. It uses an enum for different types of searches which can be expanded or whatever. mine needs to be a little more intense than this requiring some additional parameters for where to search, but i just wanted to test the concept and store it somewhere before I went to bed and forgot about it.
Code:
Public Class CustomPredicate
Public Enum searches As Integer
Contains = 1
IsEqualTo = 2
DoesntContain = 3
End Enum
Public FilterKey As String
Public Pred As Predicate(Of String)
Public Sub New(ByVal type As searches,Optional ByVal Key as String = Nothing)
If Not(Key Is Nothing) Then
FilterKey = Key
End If
Select Case type
Case searches.Contains
Pred = AddressOf Contains
Case searches.DoesntContain
Pred = AddressOf DoesntContain
Case searches.IsEqualTo
Pred = AddressOf IsEqualTo
End Select
End Sub
Public Function Contains(ByVal s As String) As Boolean
Return InStr(s.ToLower, FilterKey.ToLower, CompareMethod.Binary) > 0
End Function
Public Function DoesntContain(ByVal s As String) As Boolean
Return Not (InStr(s.ToLower, FilterKey.ToLower, CompareMethod.Binary) > 0)
End Function
Public Function IsEqualTo(ByVal s As String) As Boolean
If s.ToLower = FilterKey.ToLower Then
Return True
Else
Return False
End If
End Function
End Class
Here's how to use it. Code:
Dim l as New List(Of String)
l.Add("1")
l.Add("2")
l.Add("3")
l.Add("deez")
l.Add("doze")
Dim myPredicate As New CustomPredicate(CustomPredicate.searches.Contains)
myPredicate.FilterKey = "e"
Dim str() as String = l.FindAll(myPredicate.Pred)
'str(0) = "deez" str(1) = "doze"
'another example
Dim str2() as String = l.FindAll(New CustomPredicate(CustomPredicate.searches.DoesntCon tain, "e").Pred)
'str(0) = "1" str(1) = "2" str(2) = "3"
__________________
werD, MCSD .Net <% ASP,.NET App Development %> Really Help People with Real Diseases... And Get a Cool Blue Flower! If a post has helped you please use the scales... we all need bigger heads
Last edited by werD : July 30th, 2007 at 10:14 AM. |
|
#2
|
||||
|
||||
|
this deserve place of honor in the Code Bank - thanks for sharing!
![]() |
|
#3
|
||||
|
||||
|
Quote:
thread and moved to the Code Bank Forum. thanks! ![]() |
|
#4
|
||||
|
||||
|
Cheers To That Shadow! I was looking for that post to add the ability to initialize the filterkey to make it a one line call, and was like, "Did my post disappear?". It's nice to know it will be easily available./
Thanks hope it helps someone else as well |
|
#5
|
||||
|
||||
|
lol post #3 should have come in the original thread, my bad..
![]() glad you found this thread and thanks again! ![]() |
![]() |
| Viewing: ASP Free Forums > Programming > Code Bank > Custom Predicate |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|