Using Find Method in Generic Controls With VB.NET - T List<T>.Find(<T> item)
When using VB.NET, and try to search for element in class List, there’s no elegant way for doing that(as with C# ex. List.Find(delegate (T t) { return item == t; } ); )
Reason for that is VB.NET doesn't support anonymous delegates. You could do what the C# compiler does, create a little helper class.
Follows example in VB.NET for List<string>:
Sub Main()
Dim list As New List(Of String)(3)
list.Add("C++")
list.Add("C#")
list.Add("VB.NET")
'search for 'java' in list
Dim java As New HelpFind("HelpFind")
Dim result As String = list.Find(AddressOf java.Find)
'if 'java' is not found message box is displayed
If result Is Nothing Then
Console.WriteLine("java hasn't been found!")
End If
' search for 'C#' in list
Dim csh As New HelpFind("C#")
result = list.Find(AddressOf csh.Find)
' if 'C#' is found message box is displayed
If (result IsNot Nothing) Then
Console.WriteLine("C# has been found!")
End If
End Sub
Class HelpFind
Public _str As String
Public Sub New(ByVal aVal)
_str = aVal
End Sub
Public Function Find(ByVal aVal As String) As Boolean
Return _str.Equals(aVal)
End Function
End Class