Reputation: 18812
Dim count As Func(Of Integer, Boolean) = Function(x As Integer) x = 1
If (count(GetSelectedCount())) Then
'Proceed
Else
MessageBox.Show("You can only select one item at a time.", "Multiple items selected", MessageBoxButtons.OK)
End If
GetSelectedCount returns the number of items checkemarked in a grid. It returns 0 when nothing is selected. The Lambda is supposed to return true only when 1 item is selected. The messagebox should only run when > 1 items are selected. I ma getting the messagebox even when no items are selected.
Solution ~ Decided to drop the Lambda and go old school
Select Case GetSelectedCount()
Case 1
Case Is > 1
MessageBox.Show("You can only select one item at a time.", "Multiple Selection", MessageBoxButtons.OK)
Case Else
MessageBox.Show("You have no items selected.", "No Selection", MessageBoxButtons.OK)
End Select
Upvotes: 0
Views: 175
Reputation: 11662
Your lambda function (checks whether ONE item is selected) and your stated goal (run message box if >1 items are selected) are not mutually exclusive. Neither covers the case when NO items are selected.
So if no items are selected, then "x=1" is false, so the "If" statement fails and you fall through to the message box.
What about writing
Dim count As Func(Of Integer, Boolean) = Function(x As Integer) (x <= 1)
??
Upvotes: 2
Reputation: 416149
The = operator pulls double duty for both assignment and equality in VB.Net. Is it possible it's being interpreted incorrectly as assignment here? Try this instead:
Dim count As Func(Of Integer, Boolean) = Function(x As Integer) Return x = 1
Upvotes: 2