我可以在 VB.NET 中实现 IEnumerable 函数的收益返回吗?

发布于 2024-11-14 07:30:40 字数 1538 浏览 3 评论 0 原文

可能的重复:
VB.NET 中的产量

在 C# 中,当编写返回 IEnumerbleIEnumerble< 的函数时, >,您可以使用 yield return 返回枚举的单个项目,并使用 yield break; 表示没有剩余项目。执行相同操作的 VB.NET 语法是什么?

NerdDinner 代码中的示例:

public IEnumerable<RuleViolation> GetRuleViolations() {

   if (String.IsNullOrEmpty(Title))
       yield return new RuleViolation("Title required","Title");

   if (String.IsNullOrEmpty(Description))
       yield return new RuleViolation("Description required","Description");

   if (String.IsNullOrEmpty(HostedBy))
       yield return new RuleViolation("HostedBy required", "HostedBy");

   if (String.IsNullOrEmpty(Address))
       yield return new RuleViolation("Address required", "Address");

   if (String.IsNullOrEmpty(Country))
       yield return new RuleViolation("Country required", "Country");

   if (String.IsNullOrEmpty(ContactPhone))
       yield return new RuleViolation("Phone# required", "ContactPhone");

   if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
       yield return new RuleViolation("Phone# does not match country", "ContactPhone");

   yield break;
}

将 C# 转换为 VB.NET 工具 给出“YieldStatement 不受支持”错误。

Possible Duplicate:
Yield In VB.NET

In C#, when writing a function that returns an IEnumerble<>, you can use yield return to return a single item of the enumeration and yield break; to signify no remaining items. What is the VB.NET syntax for doing the same thing?

An example from the NerdDinner code:

public IEnumerable<RuleViolation> GetRuleViolations() {

   if (String.IsNullOrEmpty(Title))
       yield return new RuleViolation("Title required","Title");

   if (String.IsNullOrEmpty(Description))
       yield return new RuleViolation("Description required","Description");

   if (String.IsNullOrEmpty(HostedBy))
       yield return new RuleViolation("HostedBy required", "HostedBy");

   if (String.IsNullOrEmpty(Address))
       yield return new RuleViolation("Address required", "Address");

   if (String.IsNullOrEmpty(Country))
       yield return new RuleViolation("Country required", "Country");

   if (String.IsNullOrEmpty(ContactPhone))
       yield return new RuleViolation("Phone# required", "ContactPhone");

   if (!PhoneValidator.IsValidNumber(ContactPhone, Country))
       yield return new RuleViolation("Phone# does not match country", "ContactPhone");

   yield break;
}

This convert C# to VB.NET tool gives a "YieldStatement is unsupported" error.

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

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

发布评论

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

评论(6

原来分手还会想你 2024-11-21 07:30:40

目前,从语言语法层面来看,VB.Net 中还没有与 C# 的yield return 等效的方法。

然而,Bill McCarthy 最近在 MSDN 杂志上发表了一篇关于如何在 VB.Net 9.0 中实现类似模式的文章

There is currently no equivalent to C#'s yield return in VB.Net from a language syntax level.

However there was a recent write up in MSDN magazine by Bill McCarthy on how to implement a similar pattern in VB.Net 9.0

情未る 2024-11-21 07:30:40

新的异步 CTP 包括对 Yield 的支持在 VB.NET 中。

有关使用信息,请参阅 Visual Basic 中的迭代器

The new Async CTP includes support for Yield in VB.NET.

See Iterators in Visual Basic for information on usage.

计㈡愣 2024-11-21 07:30:40

请在此处查看我的答案:

总结一下:
VB.Net 没有yield,但 C# 通过将代码转换为幕后的状态机来实现yield。 VB.Net 的 Static 关键字还允许您在函数中存储状态,因此理论上您应该能够实现一个类,该类允许您在用作 Static 时编写类似的代码code> 方法的成员。

See my answers here:

To summarize:
VB.Net does not have yield, but C# implements yield by converting your code to a state machine behind that scenes. VB.Net's Static keyword also allows you to store state within a function, so in theory you should be able to implement a class that allows you to write similar code when used as a Static member of a method.

忆伤 2024-11-21 07:30:40

VB.NET 中没有收益回报:(
只需创建一个列表并将其返回即可。

No yield return in VB.NET :(
Just create a list and return it.

昨迟人 2024-11-21 07:30:40

在幕后,编译器创建一个枚举器类来完成这项工作。由于 VB.NET 没有实现此模式,因此您必须创建自己的 IEnumerator(Of T) 实现

Behind the scenes, the compiler creates an enumerator class to do the work. Since VB.NET does not implement this pattern, you have to create your own implementation of IEnumerator(Of T)

简单 2024-11-21 07:30:40

下面给出输出: 2, 4, 8, 16, 32

在 VB 中,

Public Shared Function setofNumbers() As Integer()

    Dim counter As Integer = 0
    Dim results As New List(Of Integer)
    Dim result As Integer = 1
    While counter < 5
        result = result * 2
        results.Add(result)
        counter += 1
    End While
    Return results.ToArray()
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each i As Integer In setofNumbers()
        MessageBox.Show(i)
    Next
End Sub

在 C# 中

   private void Form1_Load(object sender, EventArgs e)
    {
        foreach (int i in setofNumbers())
        {
            MessageBox.Show(i.ToString());
        }
    }

    public static IEnumerable<int> setofNumbers()
    {
        int counter=0;
        //List<int> results = new List<int>();
        int result=1;
        while (counter < 5)
        {
          result = result * 2;
          counter += 1;
          yield return result;
        }
    }

Below gives output: 2, 4, 8, 16, 32

In VB,

Public Shared Function setofNumbers() As Integer()

    Dim counter As Integer = 0
    Dim results As New List(Of Integer)
    Dim result As Integer = 1
    While counter < 5
        result = result * 2
        results.Add(result)
        counter += 1
    End While
    Return results.ToArray()
End Function

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    For Each i As Integer In setofNumbers()
        MessageBox.Show(i)
    Next
End Sub

In C#

   private void Form1_Load(object sender, EventArgs e)
    {
        foreach (int i in setofNumbers())
        {
            MessageBox.Show(i.ToString());
        }
    }

    public static IEnumerable<int> setofNumbers()
    {
        int counter=0;
        //List<int> results = new List<int>();
        int result=1;
        while (counter < 5)
        {
          result = result * 2;
          counter += 1;
          yield return result;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文