【问题标题】:Error using Yield in VB.NET - Method arguments must be enclosed in parentheses在 VB.NET 中使用 Yield 时出错 - 方法参数必须用括号括起来
【发布时间】:2018-12-08 18:30:03
【问题描述】:

我正在尝试创建一个递归函数来返回 IEnmerable(Of Control) 中控件的所有后代。我创建了一个返回IEnumerable(Of Control) 的函数并使用了Yield

Public Function GetControls(C As Control) As IEnumerable(Of Control)
    For Each Child As Control In C.Controls
        Yield Child
        For Each GrandChild In GetControls(Child)
            Yield GrandChild
        Next
    Next
End Function

但是我有一个编译时错误:

错误 BC30800,方法参数必须用括号括起来。

我尝试像函数 Yield(Child)Yield Return ChildReturn Yield Child 一样使用它,但仍然出现错误。

通过在 google 或 bing 中搜索错误消息,我找不到与该问题相关的任何内容。我该如何解决这个问题?

【问题讨论】:

  • 您使用的是哪个版本的 VB/.NET/Visual Studio?
  • @StevenDoggart 我正在使用 Visual Studio 2017 .NET 4.6.2

标签: .net vb.net ienumerable yield


【解决方案1】:

在VB.NET中使用Yield语句时,函数应定义为Iterator:

Public Iterator Function GetControls(C As Control) As IEnumerable(Of Control)
    For Each Child As Control In C.Controls
        Yield Child
        For Each GrandChild In GetControls(Child)
            Yield GrandChild
        Next
    Next
End Function

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-07
    • 2018-12-18
    • 1970-01-01
    • 2018-02-26
    • 2019-02-13
    • 2017-11-05
    • 2015-06-26
    相关资源
    最近更新 更多