Menerjemahkan "pengembalian hasil" ke VB

pemrograman

[ad_1]

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.

Apa yang saya coba:

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

Solusi 1

Solusi 2

Saya tidak akan mengatakan bahwa itu adalah “bukan pekerjaan sederhana”, hanya dilakukan secara berbeda dengan C#. Begini caranya: Iterator – Visual Dasar | Microsoft Belajar[^]

Jadi, kira-kira (tidak diuji), versi VB.Net seharusnya:

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

[ad_2]

コメント

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