我发现在VS2010实际发布之前,this answer被问及并回答了问题。
他们说
VB9只有单行匿名 功能。我们正在添加完整的声明 和VB10中的多行lambda。
但我尝试添加此代码
Dim test2 = Function(t1 As T, t2 As T) (
Dim val1 As IComparable = DirectCast(prop.GetValue(t1), IComparable)
Dim val2 As IComparable = DirectCast(prop.GetValue(t2), IComparable)
Return val1.CompareTo(val2)
)
到Visual Studio 2010中的.NET Framework 4.0项目,它不能编译。
如果这个功能真的实现了,我做错了吗?
答案 0 :(得分:12)
我相信你只是错过了'结束功能'系列。试试这个:
Dim test2 = (Function(t1 As T, t2 As T)
Dim val1 As IComparable = DirectCast(prop.GetValue(t1), IComparable)
Dim val2 As IComparable = DirectCast(prop.GetValue(t2), IComparable)
Return val1.CompareTo(val2)
End Function)
答案 1 :(得分:3)
您缺少End Function
,并且您试图将函数体括在括号中,这是错误的。这应该有效:
Dim test2 = Function(t1 As T, t2 As T)
Dim val1 As IComparable = DirectCast(prop.GetValue(t1), IComparable)
Dim val2 As IComparable = DirectCast(prop.GetValue(t2), IComparable)
Return val1.CompareTo(val2)
End Function
此功能记录在此处:
答案 2 :(得分:2)
以下是您可能会觉得有用的内容。注意如何立即调用声明的方法。
Dim food = New With {
.ID = 1,
.Name = "Carrot",
.Type = (
Function(name As String)
If String.IsNullOrEmpty(name) Then Return String.Empty
Select Case name.ToLower()
Case "apple", "tomato": Return "Fruit"
Case "potato": Return "Vegetable"
End Select
Return "Meat"
End Function
)(.Name)
}
Dim type = food.Type