Skip to content

@dieseltravis /gist:295062
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

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
Something went wrong with that request. Please try again.