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

发布于 2024-09-26 00:59:06 字数 588 浏览 0 评论 0原文

我发现这个答案在VS2010实际发布之前就被问到并得到了回答。

他们说

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 项目中,但它无法编译。

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

I found that this answer was asked and answered before VS2010 was actually released.

They say that

VB9 has only single-line anonymous
functions. We're adding full statement
and multi-line lambdas in VB10.

But I tried to add this code

 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)
 )

to a .NET Framework 4.0 project in Visual Studio 2010 and it does not compile.

Do you now if this feature is really implemented and what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

讽刺将军 2024-10-03 00:59:06

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

 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)

I believe you are only missing your 'End Function' line. Try this:

 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)
那片花海 2024-10-03 00:59:06

您缺少 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

此功能记录在此处:

You are missing End Function and you are trying to enclose the function body in parenthesis, which is wrong. This should work:

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

This feature is documented here:

沙沙粒小 2024-10-03 00:59:06

以下是您可能会觉得有用的东西。请注意如何立即调用声明的方法。

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

Here is something you might find useful. Note how the method declared is instantly invoked.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文