What is the VB.net equivalent to await foreach
found in C#?
Based on what i have found VB.net's For Each
cannot use the Await
operator (Await Operator (Visual Basic))
For example how would this C# example be converted to VB.net (List blobs with Azure Storage client libraries)?
private static async Task ListBlobsFlatListing(BlobContainerClient blobContainerClient,
int? segmentSize)
{
try
{
// Call the listing operation and return pages of the specified size.
var resultSegment = blobContainerClient.GetBlobsAsync()
.AsPages(default, segmentSize);
// Enumerate the blobs returned for each page.
await foreach (Azure.Page<BlobItem> blobPage in resultSegment)
{
foreach (BlobItem blobItem in blobPage.Values)
{
Console.WriteLine("Blob name: {0}", blobItem.Name);
}
Console.WriteLine();
}
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
CodePudding user response:
You probably need to write the equivalent code that a For Each
turns into. For a conventional (synchronous) For Each
loop:
For Each item In Sequence
'Do whatever with item
Next
Is equivalent to:
Dim iterator = Sequence.GetEnumerator()
Do While iterator.MoveNext()
Dim item = iterator.Current
'Do whatever with item
End Do
I would expect the transformation for an async enumerable to be essentially the same.
Dim iterator As IAsyncEnumerator(Of Object) 'Or appropriate type
Try
iterator = AsyncSequence.GetAsyncEnumerator()
Do While Await iterator.MoveNextAsync()
Dim item = iterator.Current
'Do whatever with item
End Do
Finally
Await iterator.DisposeAsync()
End Try
It's not quite as clean as if you could write Await For Each
(and have proper Using
support) but it's not too far off.