这个Lambda的处理是什么?
问题描述:
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返回网格中checkemarked项的数量。当没有选择任何东西时它返回0。只有在选择1个项目时,Lambda才会返回true。只有当选择> 1项目时,消息框才应该运行。即使在没有选择任何项目的情况下,我也可以获取消息框。这个Lambda的处理是什么?
解决〜决定放弃Lambda和去老同学
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
答
=运算符拉在VB.Net两种分配和平等的双重任务。它有可能被错误地解释为转让吗?试试这个:
Dim count As Func(Of Integer, Boolean) = Function(x As Integer) Return x = 1
答
你的lambda函数(检查一个项目是否处于选中状态)和你的既定目标(如果选择了> 1项运行消息框)并不相互排斥。当没有项目被选中时,这两种情况都不会涵盖。
所以如果没有项目被选中,那么“x = 1”是错误的,所以“If”语句失败并且你进入消息框。
有关编写
Dim count As Func(Of Integer, Boolean) = Function(x As Integer) (x <= 1)
什么?
+0
用“预期表达式”来加下蓝色,但您对逻辑 – 2009-01-27 22:04:15
Na,用“预计表达”加下划线的蓝色。 – 2009-01-27 22:01:18