Skip to content

Instantly share code, notes, and snippets.

@dieseltravis
Created February 4, 2010 20:27
Show Gist options
  • Save dieseltravis/295062 to your computer and use it in GitHub Desktop.
Save dieseltravis/295062 to your computer and use it in GitHub Desktop.
using List(Of T).Find(Predicate) in VB.Net 2.0
' 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
// 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; });
}
}
@tmsimont
Copy link

tmsimont commented Dec 4, 2014

shouldn't this:

    ' 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

Be this?

    ' a filtering function to pass into List.Find() as a Predicate
    Public Function FindById(ByVal v As Video) As Boolean
        Return v.Id.Equals(_videoSearchID)
    End Function

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment