将yield 翻译成VB.NET

发布于 2024-09-25 08:51:32 字数 2486 浏览 7 评论 0原文

首先,我必须假设我对 C# 的 Yield 关键字及其功能不太熟悉。 将其“翻译”为 VB.NET 的最佳/最简单方法是什么? 特别是我尝试将此代码转换为VB.NET,但是我失败了:

yield return new MatchNode(++index, current.Value);

我所拥有的是:

Imports System.Collections
Imports System.Data.SqlTypes
Imports System.Diagnostics.CodeAnalysis
Imports System.Text.RegularExpressions
Imports Microsoft.SqlServer.Server

Class MatchNode
    Private _index As Integer
    Private _value As String

    Public Sub New(ByVal index As Integer, ByVal value As String)
        _index = index
        _value = value
    End Sub

    Public ReadOnly Property Index() As Integer
        Get
            Return _index
        End Get
    End Property

    Public ReadOnly Property Value() As String
        Get
            Return _value
        End Get
    End Property

End Class

Class MatchIterator
    Implements IEnumerable

    Private _regex As Regex
    Private _input As String

    Public Sub New(ByVal input As String, ByVal pattern As String)
        MyBase.New()
        _regex = New Regex(pattern, UserDefinedFunctions.Options)
        _input = input
    End Sub

    Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Dim index As Integer = 0
        Dim current As Match = Nothing

        While (current Is Nothing OrElse current.Success)
            If current Is Nothing Then
                current = _regex.Match(_input)
            Else
                current = current.NextMatch()
            End If

            If current.Success Then
                index += 1
                'following should be a VB.Net yield'
                Return New MatchNode(index, current.Value)
            End If

        End While
    End Function
End Class

Partial Public Class UserDefinedFunctions

    <SqlFunction(FillRowMethodName:="FillMatchRow", TableDefinition:="[Index] int,[Text] nvarchar(max)")> _
    Public Shared Function RegexMatches(ByVal input As SqlChars, ByVal pattern As SqlString) As IEnumerable
        Return New MatchIterator(New String(input.Value), pattern.Value)
    End Function

    Public Shared Sub FillMatchRow(ByVal data As Object, ByRef index As SqlInt32, ByRef text As SqlChars)
        Dim node As MatchNode = CType(data, MatchNode)
        index = New SqlInt32(node.Index)
        text = New SqlChars(node.Value.ToCharArray)
    End Sub

End Class

first i must assume that i'm not very familiar with the C# yield keyword and its function.
What is the best/easiest way to "translate" it into VB.NET?
Especially i tried to convert this code into VB.NET, but i failed with:

yield return new MatchNode(++index, current.Value);

What i have is:

Imports System.Collections
Imports System.Data.SqlTypes
Imports System.Diagnostics.CodeAnalysis
Imports System.Text.RegularExpressions
Imports Microsoft.SqlServer.Server

Class MatchNode
    Private _index As Integer
    Private _value As String

    Public Sub New(ByVal index As Integer, ByVal value As String)
        _index = index
        _value = value
    End Sub

    Public ReadOnly Property Index() As Integer
        Get
            Return _index
        End Get
    End Property

    Public ReadOnly Property Value() As String
        Get
            Return _value
        End Get
    End Property

End Class

Class MatchIterator
    Implements IEnumerable

    Private _regex As Regex
    Private _input As String

    Public Sub New(ByVal input As String, ByVal pattern As String)
        MyBase.New()
        _regex = New Regex(pattern, UserDefinedFunctions.Options)
        _input = input
    End Sub

    Public Function GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Dim index As Integer = 0
        Dim current As Match = Nothing

        While (current Is Nothing OrElse current.Success)
            If current Is Nothing Then
                current = _regex.Match(_input)
            Else
                current = current.NextMatch()
            End If

            If current.Success Then
                index += 1
                'following should be a VB.Net yield'
                Return New MatchNode(index, current.Value)
            End If

        End While
    End Function
End Class

Partial Public Class UserDefinedFunctions

    <SqlFunction(FillRowMethodName:="FillMatchRow", TableDefinition:="[Index] int,[Text] nvarchar(max)")> _
    Public Shared Function RegexMatches(ByVal input As SqlChars, ByVal pattern As SqlString) As IEnumerable
        Return New MatchIterator(New String(input.Value), pattern.Value)
    End Function

    Public Shared Sub FillMatchRow(ByVal data As Object, ByRef index As SqlInt32, ByRef text As SqlChars)
        Dim node As MatchNode = CType(data, MatchNode)
        index = New SqlInt32(node.Index)
        text = New SqlChars(node.Value.ToCharArray)
    End Sub

End Class

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

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

发布评论

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

评论(5

原谅我要高飞 2024-10-02 08:51:32

由于VB.NET不提供迭代器块,因此您必须手动编写迭代器类,这是非常痛苦的。我将尝试用 C# 为您(手动)编写它,这样您就可以明白我的意思...就像这样:

internal class MatchIterator : IEnumerable
{
    private class MatchEnumerator : IEnumerator
    {
        int index = 0;
        private Match currentMatch;
        private MatchNode current;
        readonly Regex regex;
        readonly string input;
        public MatchEnumerator(Regex regex, string input)
        {
            this.regex = regex;
            this.input = input;
        }
        public object Current { get { return current; } }

        public void Reset() { throw new NotSupportedException(); }
        public bool MoveNext()
        {
            currentMatch = (currentMatch == null) ? regex.Match(input) : currentMatch.NextMatch();
            if (currentMatch.Success)
            {
                current = new MatchNode(++index, currentMatch.Value);
                return true;
            }
            return false;
        }
    }
    private Regex _regex;
    private string _input;

    public MatchIterator(string input, string pattern)
    {
        _regex = new Regex(pattern, UserDefinedFunctions.Options);
        _input = input;
    }

    public IEnumerator GetEnumerator()
    {
        return new MatchEnumerator(_regex, _input);
    }
}

Since VB.NET doesn't provide iterator blocks, you will have to write the iterator class by hand, which is extremely painful. I'll try to write it (manually) in C# for you, so you can see what I mean... like so:

internal class MatchIterator : IEnumerable
{
    private class MatchEnumerator : IEnumerator
    {
        int index = 0;
        private Match currentMatch;
        private MatchNode current;
        readonly Regex regex;
        readonly string input;
        public MatchEnumerator(Regex regex, string input)
        {
            this.regex = regex;
            this.input = input;
        }
        public object Current { get { return current; } }

        public void Reset() { throw new NotSupportedException(); }
        public bool MoveNext()
        {
            currentMatch = (currentMatch == null) ? regex.Match(input) : currentMatch.NextMatch();
            if (currentMatch.Success)
            {
                current = new MatchNode(++index, currentMatch.Value);
                return true;
            }
            return false;
        }
    }
    private Regex _regex;
    private string _input;

    public MatchIterator(string input, string pattern)
    {
        _regex = new Regex(pattern, UserDefinedFunctions.Options);
        _input = input;
    }

    public IEnumerator GetEnumerator()
    {
        return new MatchEnumerator(_regex, _input);
    }
}
演多会厌 2024-10-02 08:51:32

你必须问自己我真的要编辑这段代码吗?如果答案是否定的或不是很多,那么就不要打扰,将其保留为 C# 代码。

我是一名 VB.NET 开发人员,接下来我放弃了将 C# 代码从网络转换为 VB.NET。我只是有一个用于将代码转储到的所需项目的 C# 库。只有当我发现我需要定期/大量开发代码时,我才会经历将其转换为 VB.NET 的痛苦。

You have to ask yourself am I really going to edit this code? If the answer is no or not very much then don't bother, leave it as C# code.

I'm a VB.NET developer and have next to given up on converting C# code from the net into VB.NET. I simply have a C# library for the required project that I dump code into. It is only if I find I need to regularly/heavily develop the code that I go through the pain of converting it to VB.NET.

护你周全 2024-10-02 08:51:32

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

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

现在它包含在 .NET 4.5 和 VS 2012 中

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

See Iterators in Visual Basic for information on usage.

And now it's included in .NET 4.5 and VS 2012.

送君千里 2024-10-02 08:51:32

如果您真的非常想手动实现迭代器类,我建议您阅读本章首先,阅读 Jon Skeet 撰写的“c# 深度剖析”,了解 c# 编译器如何使用这个小小的 yield 关键字。

If you really, really want to implement the iterator class by hand, i recommend reading this chapter of "c# in depth" by Jon Skeet first to get an idea what the c#-compiler does with this little yield keyword.

天荒地未老 2024-10-02 08:51:32

Bill McCarthy 发表了这篇 好文章在 Visual Studio 杂志中,通过实现 IEnumerable(Of T)IEnumerator(Of T) 在 VB.NET 中模拟 yield

There's this nice article by Bill McCarthy in Visual Studio magazine on emulating yield in VB.NET by implementing IEnumerable(Of T) and IEnumerator(Of T).

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