List.Find Method in a VB.NET2

723 views Asked by At

Using VB in .NET2

Public Class GroupSID 
    Private _groupName As String 
    Private _sid As String 
    Public Property GroupName() As String 
        Get 
            Return _groupName 
        End Get 
        Set(ByVal value As String) 
            _groupName = value 
        End Set 
    End Property 
    Public Property SID() As String 
        Get 
            Return _sid 
        End Get 
        Set(ByVal value As String) 
            _sid = value 
        End Set 
    End Property 
End Class

After the list is populated I want to find the item with the matching groupName (there will be only 1)

Something like (pseudo VB/C#)

'Dim result As GroupSID = ListOfGroupSID.Find(x => x.GroupName == groupName) 

https://msdn.microsoft.com/en-us/library/x0b5b5bc(v=vs.80).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

from: http://www.codeproject.com/Articles/388257/Csharp-Tips-Using-delegate-in-List-Find-predicate

' expression expected error on Function(p) 
 Dim result As GroupSID = ListOfGroupSID.Find(Function(p) p.GroupName = groupName) 

Problem is VB8/.NET2 doesn't allow this..

1

There are 1 answers

1
Sehnsucht On BEST ANSWER

Anonymous function (lambda) aren't available in VB8/.Net2 so you have to define your predicate as a separate method :

Function BelongsToSameGroup(ByVal group As GroupSID) As Boolean
    Return group.GroupName = groupName ' need to be accessible
End Function

' usage
Dim result As GroupSID = ListOfGroupSID.Find(AddressOf BelongsToSameGroup)