Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# VB.NET中收益率的翻译_C#_Vb.net_Ienumerable_Yield - Fatal编程技术网

C# VB.NET中收益率的翻译

C# VB.NET中收益率的翻译,c#,vb.net,ienumerable,yield,C#,Vb.net,Ienumerable,Yield,首先,我必须假设我对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 Sy

首先,我必须假设我对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
导入系统集合
导入System.Data.SqlTypes
导入System.Diagnostics.CodeAnalysis
导入System.Text.RegularExpressions
导入Microsoft.SqlServer.Server
类匹配节点
私有_索引为整数
私有_值作为字符串
Public Sub New(ByVal索引为整数,ByVal值为字符串)
_索引=索引
_价值=价值
端接头
作为整数的公共只读属性索引()
得到
返回索引
结束
端属性
作为字符串的公共只读属性值()
得到
返回值
结束
端属性
末级
类匹配迭代器
可数的
Private _regex作为regex
私有输入作为字符串
Public Sub New(ByVal输入为字符串,ByVal模式为字符串)
MyBase.New()
_regex=newregex(模式,UserDefinedFunctions.Options)
_输入=输入
端接头
作为IEnumerator的公共函数GetEnumerator()实现IEnumerable.GetEnumerator
作为整数的Dim索引=0
匹配时变暗电流=无
While(current不算什么,或者lse current.Success)
如果电流为零,那么
当前=_regex.Match(_输入)
其他的
当前=当前。下一个匹配()
如果结束
如果是现在,那就成功吧
指数+=1
“以下是VB.Net的收益率”
返回新的MatchNode(索引,当前值)
如果结束
结束时
端函数
末级
部分公共类UserDefinedFunctions
_
公共共享函数RegexMatches(ByVal输入为SqlChars,ByVal模式为SqlString)作为IEnumerable
返回新的MatchIterator(新字符串(input.Value)、pattern.Value)
端函数
公共共享子FillMatchRow(ByVal数据作为对象,ByRef索引作为SqlInt32,ByRef文本作为SqlChars)
作为MatchNode的Dim节点=CType(数据,MatchNode)
index=newsqlint32(node.index)
text=新的SqlChars(node.Value.tocharray)
端接头
末级

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

如果您真的非常想手工实现迭代器类,我建议您先阅读Jon Skeet的“c#indepth”,了解c#编译器使用这个小小的yield关键字做什么。

您必须问问自己,我真的要编辑这段代码吗?如果答案是否定的或不是很重要,那么不要麻烦,把它作为C代码

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

这是Bill McCarthy在Visual Studio杂志上写的关于通过实现
IEnumerable(of T)
IEnumerator(of T)
在VB.NET中模拟
yield

新版本包括在VB.NET中对
Yield
的支持

有关用法的信息,请参阅


现在。

你看到这个了吗?我看到了它们,但我不知道怎么做,错过了一个例子。我希望会有一个类似的简单方法,几乎相同,但没有整个收益功能(惰性评估)。这很好,因为我并不真正需要它作为VB.Net,而只需要SQL-Server中的dll。谢谢。我仍然有一个问题,sql server使用此函数始终返回一个空结果集。但是,我不会花更多时间将C#转换为VB.Net,而是按照Tim Murphy的建议将其保留为C#。另一种方法是使用
GetEnumerator()
方法,该方法创建
T[]
List
并返回该数组或列表的枚举数。当然,这种方法的语义不同于惰性计算的语义,但它仍然是一种有用的技巧(即使在C#中,如果要枚举集合的快照,这种方法也比例如将其复制到数组/列表并使用
yield return
迭代该数组/列表的元素要好).Update-他们在版本11中将迭代器块添加到VB.NET中