Home > Blockchain >  how to use lambda in vb.net
how to use lambda in vb.net

Time:09-21

When I use AngleSharp in VB.NET, the lambda in C# becomes a question. VB.NET can use function to replace lambda but seem to accept only one argument

Dim document = Await context.OpenAsync(Function(Res)
Res.Content("

")
.Address("http://localhost/")
.Header(HeaderNames.SetCookie, my_headers)
End Function
)

The errors:

Error BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Leading '.' or '!' can only appear inside a 'With' statement.
Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type.

How do I fix it?

the errors after modifying by 41686d6564

Error   BC30518 Overload resolution failed because no accessible 'OpenAsync' can be called with these arguments:
    Extension method 'Public Function OpenAsync(response As IResponse, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'IResponse' because 'IResponse' is not a delegate type.
    Extension method 'Public Function OpenAsync(request As DocumentRequest, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'DocumentRequest' because 'DocumentRequest' is not a delegate type.
    Extension method 'Public Function OpenAsync(url As Url, [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'Url' because 'Url' is not a delegate type.
    Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'Return' statement in a Sub or a Set cannot return a value.
    Extension method 'Public Function OpenAsync(request As Action(Of VirtualResponse), [cancel As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': 'HeaderNames' is not declared. It may be inaccessible due to its protection level.
    Extension method 'Public Function OpenAsync(address As String, [cancellation As CancellationToken = Nothing]) As Task(Of IDocument)' defined in 'BrowsingContextExtensions': Lambda expression cannot be converted to 'String' because 'String' is not a delegate type. 

CodePudding user response:

There are two problems with your code:

  1. The overload that you're trying to use takes an Action, not a Func. Therefore, you ought to use Sub instead of Function.

  2. In VB, the dot has to come at the end of the line, not at the beginning of the next one (in C#, you may do either), or else the line continuation character (i.e., _) must be used.

Changing your code to something like the following should make it compile:

Dim document = Await context.OpenAsync(
    Sub(Res)
        Res.Content("").
        Address("http://localhost/").
        Header(HeaderNames.SetCookie, my_headers)
    End Sub)

Or using explicit line continuation:

Dim document = Await context.OpenAsync(
    Sub(Res)
        Res.Content("") _
        .Address("http://localhost/") _
        .Header(HeaderNames.SetCookie, my_headers)
    End Sub)
  • Related