【解決方法】翻訳中 "利回り" VBへ


Hello everyone, I have another translation problem that not even - chat.openai.com - could help me, I use vb 2010, and I know very well that from the 2012 version "yield" is supported, but for compatibility I have to use the 2010 version. Is there a not too far-fetched alternative to get around the problem? The code is as follows:

public IEnumerable<Range> GetRanges(string regexPattern)
         {
             var range = new Range(this);
             range.SelectAll();
             //
             foreach (Range r in range.GetRanges(regexPattern, RegexOptions.None))
                 yield return r;
         }

I hope for your help and thank you.

私が試したこと:

I tried this, but it doesn't work, the program crashes and I restart vb...

Module Module1
     Sub Main()
         For Each number In GenerateNumbers(1, 5)
             Console.WriteLine(number)
         Next
         Console.ReadLine()
     End Sub

     Function GenerateNumbers(start As Integer, count As Integer) As Integer()
         Dim result(count - 1) As Integer
         For i As Integer = 0 To count - 1
             result(i) = start + i
         Next
         Return result
     End Function
End Module

解決策 1

ここを参照してください: https://stackoverflow.com/questions/2912851/what-is-the-equivalent-syntax-in-vb-net-for-yield-return[^]

決して簡単な仕事ではありません…

解決策 2

これは「単純な仕事ではない」とは言いませんが、C# とはやり方が違うだけです。 その方法は次のとおりです。 イテレータ – Visual Basic | Microsoft Learn[^]

したがって、推測では (テストされていません)、VB.Net のバージョンは次のようになります。

VB
Public Iterator Function GetRanges(regexPattern As String) As IEnumerable(Of Range)
    Dim range As New Range(Me)
    range.SelectAll()
    
    For Each r As Range In range.GetRanges(regexPattern, RegexOptions.None)
        Yield r
    Next
End Function

コメント

タイトルとURLをコピーしました