public
Last active

using List(Of T).Find(Predicate) in VB.Net 2.0

  • Download Gist
gistfile1.vb
Visual Basic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
' VB.Net 2.0
' a class for videos is what I was using my original code for
Public Class Video
Private _id As Integer
 
Public Property Id() As Integer
Get
Return _id
End Get
Set(ByVal value As Integer)
_id = value
End Set
End Property
' ...other properties, functions, etc.
 
' a private inner class for storing the ID to look for
Private Class VideoMatcher
Private ReadOnly _videoSearchID As Integer
 
Sub New(ByVal id As Integer)
_videoSearchID = id
End Sub
 
' a filtering function to pass into List.Find() as a Predicate
Public Function FindById(ByVal v As Video) As Boolean
Return v.Id = _videoSearchID
End Function
End Class
 
Public Shared Function FindVideo(ByVal videoList As List(Of Video), ByVal id As Integer) As Video
Return videoList.Find(AddressOf New VideoMatcher(id).FindById)
End Function
End Class
gistfile2.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13
// C# version
 
public class Video
{
public int Id { get; set; }
// ...other properties, functions, etc.
 
public static Video FindVideo(List<Video> videoList, int id)
{
return videoList.Find(delegate(Video v) { return v.Id == id; });
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.