Donate

Predicate Wrapper Custom Class in VB.NET

Here's a custom predicate wrapper class I got from visual basic forums. I'll be converting and posting the C# equivalent for this.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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  

Here's the findtrack function:
1
2
3
4
Public Shared Function FindIndexTrackNumber(ByVal SysTrack As SystemTrack, ByVal searchArg As Integer) As Boolean  
     'Searches for the SystemTrack with TrackNumber searchArg  
     Return SysTrack.ID.Equals(searchArg)  
   End Function 

Here's its sample use:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
Dim SearchNumber As Integer = 456  
     Dim Index As Integer  
     Dim SystemTracks As New List(Of SystemTrack)  
     SystemTracks.Add(New SystemTrack With {.ID = 111, .Title = "Help Me", .Author = "Greg Phil"})  
     SystemTracks.Add(New SystemTrack With {.ID = 222, .Title = "Help Me1", .Author = "James Nelson"})  
     SystemTracks.Add(New SystemTrack With {.ID = 456, .Title = "Help Me2", .Author = "Tom Enigma"})  
     SystemTracks.Add(New SystemTrack With {.ID = 12, .Title = "Help Me3", .Author = "Lord Brown"})  
     Index = SystemTracks.FindIndex(New PredicateWrapper(Of SystemTrack, Integer)(SearchNumber, AddressOf SystemTrack.FindIndexTrackNumber))  
     If Index <> -1 Then  
       Console.WriteLine("Sytem Track has been found at: " & Index)  
     Else  
       Console.WriteLine("System Track not found! ")  
     End If  
     Console.ReadLine()  
:)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid