Created
February 4, 2010 20:27
-
-
Save dieseltravis/295062 to your computer and use it in GitHub Desktop.
using List(Of T).Find(Predicate) in VB.Net 2.0
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
' 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; }); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
shouldn't this:
Be this?