Reputation: 3
Did vb6 years ago. Trying to upgrade brain to VB.NET
Private Sub BtnFindAll_Click(sender As Object, e As EventArgs) Handles BtnFindAll.Click
Dim AutosArray() As String = {"Chrysler", "Corvette", "Lincoln", "Buick", "Ford", "Fiat", "Chevrolet", "Mercury"}
Dim BooBee() As String
' The below line gets the error...
BooBee = System.Array.FindAll(AutosArray, AddressOf MatchFunction)
' The below line gets the same error...
'Dim BooBee() As String = Array.FindAll(AutosArray, AddressOf MatchFunction)
End Sub
Private Function MatchFunction(ByVal strAuto As String) As String
If strAuto.IndexOf("c") > 1 Then
Return strAuto
End If
End Function
When I click on BtnFindAll I get the below error message:
Exception Unhandled System.InvalidCastException: 'Conversion from string "" to type 'Boolean' is not valid.'
Inner Exception FormatException: Input string was not in a correct format.
Upvotes: 0
Views: 114
Reputation: 216313
The MatchFunction should return a boolean to be compatible with the FindAll method
Something like this
Private Function MatchFunction(ByVal strAuto As String) As Boolean
If strAuto.IndexOf("c") > 1 Then
Return True
Else
Return False
End If
End Function
You can read this point in the Remarks section of the FindAll documentation
Consider also that now there is another option to extract that information with a single line using Linq
BooBee = AutosArray.Where(Function(x) x.IndexOf("c") > 1).ToArray()
Upvotes: 1