Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
VB.NET和Visual Studio 2010是否支持多行匿名方法?_Vb.net_Visual Studio 2010_.net 4.0 - Fatal编程技术网

VB.NET和Visual Studio 2010是否支持多行匿名方法?

VB.NET和Visual Studio 2010是否支持多行匿名方法?,vb.net,visual-studio-2010,.net-4.0,Vb.net,Visual Studio 2010,.net 4.0,我发现在VS2010实际发布之前,这个问题已经被问到并回答了 他们说 VB9只有一行匿名代码 功能。我们添加了完整的声明 以及VB10中的多行lambdas 但是我试着添加这个代码 Dim test2 = Function(t1 As T, t2 As T) ( Dim val1 As IComparable = DirectCast(prop.GetValue(t1), IComparable) Dim val2 As IComparable = DirectCast(prop.GetV

我发现在VS2010实际发布之前,这个问题已经被问到并回答了

他们说

VB9只有一行匿名代码 功能。我们添加了完整的声明 以及VB10中的多行lambdas

但是我试着添加这个代码

 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项目,但它不会编译


你现在知道这个功能是否真的实现了,我做错了什么吗?

我相信你只是错过了“结束功能”一行。试试这个:

 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)

您缺少
结束函数
,并且试图将函数体括在括号中,这是错误的。这应该起作用:

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
此处记录了此功能:


    • 以下是一些您可能会发现有用的东西。注意声明的方法是如何立即调用的

      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
      

      哦,谢谢你!对不起,这个愚蠢的问题,今天办公室可能太热了:哈哈!一点也不傻!不到一周前,我遇到了完全相同的问题。