You may have heard of VB11, currently in the VS11 developer preview. It adds two main syntax elements - Iterator functions and Async methods. I don't know much about Async methods yet, but here's a quick Iterator tutorial.
Basically, you put the Iterator keyword before the Function keyword and it becomes an iterator and returns an IEnumerable(Of t). Then, you have some kind of loop, and use the new Yield keyword to "return" a value. For example, this method generates lines from an array of points:
Code:
''' <summary>
''' Represents a line.
''' </summary>
Private Structure Line
Public Start As Point
Public [End] As Point
Public Function IsVertical() As Boolean
Return Me.Start.X = Me.End.X
End Function
Public Function IsHorizontal() As Boolean
Return Me.Start.Y = Me.End.Y
End Function
Public Function IsEmpty() As Boolean
Return Me.IsHorizontal() AndAlso Me.IsVertical()
End Function
Public Shared Operator =(ByVal a As Line, ByVal b As Line) As Boolean
Return a.Start = b.Start AndAlso a.End = b.End
End Operator
Public Shared Operator <>(ByVal a As Line, ByVal b As Line) As Boolean
Return a.Start <> b.Start OrElse a.End <> b.End
End Operator
End Structure
''' <summary>
''' Gets the lines between each point.
''' </summary>
''' <param name="points">The points for which to create lines.</param>
Private Shared Iterator Function Lines(ByVal points As IEnumerable(Of Point)) As IEnumerable(Of Line)
Dim last As Point?
For Each p As Point In points
If last IsNot Nothing Then Yield New Line With {
.Start = last.Value,
.End = p
}
last = p
Next
End Function
Here's how you might use it:
Code:
' Draw a square in a *very* roundabout way :)
' By the way, you can omit types in For Each statements in VB 11:
' v
For Each line In Lines({
Point.Empty,
New Point(100, 0),
New Point(100, 100),
New Point(0, 100),
Point.Empty
})
myGraphics.DrawLine(Pens.Red, line.Start, line.End)
Next
Use iterators well - they can be really great sometimes.
Get the CTP off of the page to use the features now in Visual Studio 2010. Even though it's still in CTP they had such a demand for the new features that they've allowed it to be used in production since CTP Version 2.
I'm struggling to see a good use for them, if I had to recreate the example you did without iterators I would simply have the function return an array of line, and then loop through the array. Not much of an improvement. I would like to see them used in a place where conventional methods would be hard or tedious.
I wasn't meaning any disrespect to your tutorial, because it is a good tutorial. But I fail to see the practical use for this function.
None taken. All I meant was that mine is not the best example, because what Yield/Iterators might be more useful in is enumeration of things that don't have a fixed length. Normally, you'd need to create an instance of a custom IEnumerable type, which would delegate to an IEnumerator in the For Each. But with Iterators, you don't have to. So something to read the contents of a stream, byte-by-byte, might look like this:
Code:
Public Shared Iterator Function IterateStream(ByVal s As Stream) As IEnumerable(Of Byte)
While s.Position < s.Length
Yield CByte(s.ReadByte())
End While
End Function
You could do some interesting things with sockets and asynchronous functions, probably.
Now I get it. You can for Each every byte in a file like It was loaded into memory, when in reality it only reads a byte each time when next is called. (The lightbulb has switched on)
None taken. All I meant was that mine is not the best example, because what Yield/Iterators might be more useful in is enumeration of things that don't have a fixed length. Normally, you'd need to create an instance of a custom IEnumerable type, which would delegate to an IEnumerator in the For Each. But with Iterators, you don't have to. So something to read the contents of a stream, byte-by-byte, might look like this:
Code:
Public Shared Iterator Function IterateStream(ByVal s As Stream) As IEnumerable(Of Byte)
While s.Position < s.Length
Yield CByte(s.ReadByte())
End While
End Function
You could do some interesting things with sockets and asynchronous functions, probably.