Exit to VB.NET

C # has the yield keyword. VB.NET is missing this keyword. How did Visual Basic programmers take advantage of the lack of this keyword? Do they implement their class of iterators? Or do they try and code to avoid the need for an iterator?

The yield keyword forces the compiler to do some encoding behind the scenes. A good example of this is the implementation of iterators in C # and its consequences (part 1) .

+62
Sep 18 '08 at 21:53
source share
7 answers

Note. This answer is old. Since then, Iterator blocks have been added to VB.NET

C # translates the yield keyword into a state machine at compile time. VB.NET does not have the yield keyword, but it has its own mechanism for safely embedding state in a function that is not easily accessible in C #.

The C # static usually translated into Visual Basic using the Shared keyword, but there are two places where things get confused. One of them is that the C # static class is really a module in Visual Basic, and not for the Shared class (you think that they will let you code it anyway in Visual Basic, but noooo). Another is that VB.NET has its own static . However, static has a different meaning in VB.NET.

You use the static in VB.NET to declare a variable inside a function, and when you execute a variable, it retains its state when calling functions. This is different from simply declaring a private static class member in C #, because a static function member in VB.NET is also guaranteed to be thread safe, as the compiler translates it to use the Monitor class at compile time.

So why write all this here? Well, it should be possible to create a reusable generic class Iterator<T> (or Iterator(Of T) in VB.NET). In this class, you would use the state machine used by C #, with the Yield() and Break() methods that match the C # keywords. Then you can use a static instance (in the sense of VB.NET) in a function so that it can ultimately do almost the same job as C # yield , in about the same amount of code (discarding the class implementation itself, since it will be infinite reused).

I did not care about Revenue to try to do it myself, but it should be doable. However, this is also far from trivial, as C # team member Eric Lippert calls this โ€œthe most complicated compiler conversion โ€. I also believed since I wrote the first draft of this year ago that it really is not possible in a meaningful way until Visual Studio 2010 appears, as it will require sending several lambdas to the Iterator class and to be really we need [.NET 4 3 support for multi-line lambdas.

+71
Dec 19 '08 at 21:00
source share

Async CTP includes Yield support in VB.NET.

For usage information, see Iterators in Visual Basic .

And now it is included in the box with Visual Studio 2012!

+21
May 05 '11 at 14:39
source share

There's a good article Using Bill McCarthy's Iterators in VB Now in Visual Studio Magazine on emulating yield in VB.NET. Alternatively, wait for the next version of Visual Basic.

+14
Sep 26 '09 at 7:22
source share

I personally just write my own iterator class, which inherits from IEnumerator (Of T). It will take some time to get it right, but I think in the end itโ€™s better to write it right and then try to avoid it. Another method I did was write a recursive method that returns IEnumerable (Of T) and just returns List (Of T) and uses .AddRange.

+1
Sep 18 '08 at 21:56
source share

Hope this will be in the past with the upcoming version of VB. Since iterators actually take on a lot of importance with new paradigms (especially LINQ combined with lazy pricing), this has a pretty high priority, as far as I know from Paul Wick's blog. Again, Paul is no longer the head of the VB team, and I have not had time to watch the PCD negotiations yet.

However, if you are interested, they are linked on the Paul blog .

0
Dec 19 '08 at 21:07
source share

Below is the exit code

2, 4, 8, 16, 32

In VB.NET,

 Public Shared Function setofNumbers() As Integer() Dim counter As Integer = 0 Dim results As New List(Of Integer) Dim result As Integer = 1 While counter < 5 result = result * 2 results.Add(result) counter += 1 End While Return results.ToArray() End Function Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load For Each i As Integer In setofNumbers() MessageBox.Show(i) Next End Sub 

In c #

 private void Form1_Load(object sender, EventArgs e) { foreach (int i in setofNumbers()) { MessageBox.Show(i.ToString()); } } public static IEnumerable<int> setofNumbers() { int counter=0; int result=1; while (counter < 5) { result = result * 2; counter += 1; yield return result; } } 
0
May 2 '11 at 9:57
source share

VB.NET has the Iterator keyword https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/modifiers/iterator

Since Visual Studio 2012 seems

0
Jun 22 '17 at 13:40
source share



All Articles