mrt 11

Geplaatst door: André Obelink (Host)
11-3-2012 10:35 

One of the language changes introduced in Visual Basic 11 to support the asynchronous programming model are Iterators. An Iterator is a construct to return already results to the calling procedure when still in a loop. The main advantage is that the calling procedure can start consuming and processing results, when the called Iterator function is not completely finished yet. Consider the code below. The first procedure calls the procedure GetFilesToProcess(). When the called procedure is finished it will loop trough the results – in this case all text files in the folder C:\ - and will process this text file. This could be code what you currently could be writing in your daily life. Perhaps you would prefer the GetFilesToProcessShort() method, but in essence they don’t differ from each other.

1: Private Sub ProcessFiles()
3:   For Each fileName In GetFileToProcess()
4:     ' Do something; process file
5:   Next
7: End Sub
9: Private Function GetFileToProcess() As List(Of String)
11:   Dim filesToProcess As New List(Of String)
13:   For Each fileName In System.IO.Directory.GetFiles("c:\", "*.txt")
14:     filesToProcess.Add(fileName)
15:   Next
17:   Return filesToProcess
19: End Function
21: 'Private Function GetFileToProcessShort() As List(Of String)
22: '  Return System.IO.Directory.GetFiles("c:\", "*.txt").ToList()
23: 'End Function

The main disadvantage of this code is – and actually we don’t know better – that the calling procedure can start after GetFiles() is finished by building the complete list of files. It would be great that the For Each in ProcessFiles() already can start processing the first file which is retrieved in GetFilesToProcess(). Fortunately you can accomplish this in Visual Basic 11 by using the Iterator and Yield keywords.

1: Private Sub ProcessFiles()
3:   For Each fileName In GetFileToProcessIterator()
4:     ' Do something; process file
5:   Next
7: End Sub
9: Private Iterator Function GetFileToProcessIterator() As IEnumerable(Of String)
11:   For Each fileName In System.IO.Directory.EnumerateFiles("c:\", "*.txt")
12:     Yield fileName
13:   Next
15: End Function

As you can see, you declare an Iterator function with the keyword Iterator . Beside of that you ensure that the result type is an IEnumarable or IEnumerable(Of T). Due the fact that we want to result a list of filenames, we choose in the code above for an IEnumerable(Of String). The most interesting part is the body of the For Each loop. We use the keyword Yield to pop the currently retrieve filename. After each Yield command, the calling procedure can already start processing the that file. Please note also that we use the ´Iterator-friendly´ variant of the method GetFiles() named EnumerateFiles().

Iterators can help you to write better responsive applications.


Happy coding!



Uw naam:
Uw email:
(Optioneel) Email wordt alleen gebruikt om een Gravatar te tonen.
Uw website:
Security Code
Neem de code over
Voeg commentaar toe   Annuleren 
Blog archive

Bataafseweg 20, 7101 PA  Winterswijk |  Postbus 428, 7100 AK Winterswijk  |  M 06-47914464  |  E info@maryor.nl