产量翻译成VB.NET [英] Translation of yield into VB.NET

查看:126
本文介绍了产量翻译成VB.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我必须假设我不是很熟悉C#产量关键字和它的功能。
什么是翻译入VB.NET最好/最简单的方法是什么?
我尤其想这段代码转换成VB .NET,但我失败:

 收益回报新MatchNode(++指数,current.Value); 



我所为:

  System.Collections中进口
进口System.Data.SqlTypes中
进口System.Diagnostics.CodeAnalysis
进口System.Text.RegularExpressions
进口Microsoft.SqlServer .Server

类MatchNode
私人_index作为整数
私人_value作为字符串

的Public Sub New(BYVAL指数为整数,BYVAL值作为字符串)
_index =指数
_value =值
端子

公共只读属性索引()作为整数
得到
返回_index
端获取
高端物业

公共只读属性值()作为字符串
得到
返回_value
端获取
高端物业

端类

类MatchIterator
实现IEnumerable

私人_regex作为正则表达式
私人_Input作为字符串

的Public Sub New(BYVAL输入作为字符串,BYVAL图案作为字符串)
MyBase.New()
_regex ​​=新的正则表达式(模式,UserDefinedFunctions.Options)
_Input =输入
端子

公共功能的GetEnumerator()作为IEnumerator的实现IEnumerable.GetEnumerator
昏暗指数为整数= 0
尺寸电流匹配=没有

虽然(目前为Nothing OrElse运算current.Success)
。如果当前是Nothing然后
电流=​​ _regex.Match(_Input)
,否则
电流=​​ current.NextMatch()
端如果

。如果current.Success然后
指数+ = 1
'之后应该是一个VB.Net产量'
返回新MatchNode(指数电流。值)
端如果

端虽然
端功能
端类

部分公共类UserDefinedFunctions

< SqlFunction(FillRowMethodName:=FillMatchRow,TableDefinition:=[索引] INT,[文字]为nvarchar(最大))> _
公共共享功能RegexMatches(BYVAL输入作为SqlChars,BYVAL图案的SqlString)为IEnumerable
返回新MatchIterator(新建字符串(input.Value),pattern.Value)
端功能

公共共享子FillMatchRow(BYVAL数据为对象,​​为ByRef指数SqlInt32,为ByRef文本SqlChars)
昏暗的节点作为MatchNode = CTYPE(数据,MatchNode)
指数=新SqlInt32( node.Index)
文本=新SqlChars(node.Value.ToCharArray)
端子

端类


解决方案

由于VB.NET不提供迭代器块,你将不得不手工编写的迭代器类,这是非常痛苦的。我会尽量把它(手动)在C#中为你写的,所以你可以看到我的意思......像这样:

 内部类MatchIterator:IEnumerable的
{
私有类MatchEnumerator:IEnumerator的
{
INT指数= 0;
私人匹配currentMatch;
私人MatchNode电流;
只读正则表达式的正则表达式;
只读字符串输入;
公共MatchEnumerator(正则表达式的正则表达式,字符串输入)
{
this.regex ​​=正则表达式;
this.input =输入;
}
公共对象当前{{返回电流; }}

公共无效复位(){抛出新NotSupportedException异常(); }
公共BOOL的MoveNext()
{
currentMatch =(currentMatch == NULL)? regex.Match(输入):currentMatch.NextMatch();
如果(currentMatch.Success)
{
电流=新MatchNode(++指数,currentMatch.Value);
返回真;
}
返回FALSE;
}
}
私人正则表达式_regex;
私人字符串_Input;

公共MatchIterator(字符串输入,字符串模式)
{
_regex ​​=新的正则表达式(模式,UserDefinedFunctions.Options);
_Input =输入;
}

公众的IEnumerator的GetEnumerator()
{
返回新MatchEnumerator(_regex,_Input);
}
}


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

解决方案

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

这篇关于产量翻译成VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆