2

In C#, I have some code that looks like:

(await GetBoolAsync()).ShouldBeTrue();

ShouldBeTrue() is an extension method from the Shouldly library that operates on Booleans.

VB.NET doesn't seem to like wrapping the Await keyword in parenthesis:

(Await GetBoolAsync()).ShouldBeTrue()

The compiler reports a syntax error on the opening parenthesis at the beginning of the line. I can work around it by declaring an intermediate variable, but is there a way to achieve this in one line like C#?

A full console application to reproduce this:

Imports System.Runtime.CompilerServices

Module Module1

    Sub Main
    End Sub

    Async Function Test() As Task
        (Await GetBoolAsync()).ShouldBeTrue()
    End Function

    Function GetBoolAsync() As Task(Of Boolean)
        Return Task.FromResult(True)
    End Function

    <Extension()>
    Public Sub ShouldBeTrue(x As Boolean)
    End Sub

End Module

The error is very unhelpful:

error BC30035: Syntax error.

svick
  • 236,525
  • 50
  • 385
  • 514
Nate Barbettini
  • 51,256
  • 26
  • 134
  • 147

1 Answers1

2

There is a special syntax for doing so - you should use the Call keyword:

Async Function Test() As Task
    Call (Await GetBoolAsync()).ShouldBeTrue()
End Function

That's because you cannot directly call a member of non-literal expression in Visual Basic, like you would in C#:

(5).ToString() 'It is wrong!

And in this particular case this includes the result of Await.

Hope that helps!

DeFazer
  • 521
  • 3
  • 17