Pergunta

I have an implementation of Task.Factory.FromAsync in C# that I would like to write in VB.NET.

This is the C# syntax.

public static Task<int> SendAsync(this Socket socket, byte[] buffer, int offset, int count)
{
        return Task.Factory.FromAsync<int>(
                       socket.BeginSend(buffer, offset, count, SocketFlags.None, null, socket),
                 socket.EndSend);
}

I have converted it to the following in VB.NET

Public Shared Function SendAsync(socket As Socket, buffer As Byte(), offset As Integer, count As Integer) As Task(Of Integer)
    Return Task.Factory.FromAsync(Of Integer)(socket.BeginSend(buffer, offset, count, SocketFlags.None, Nothing, socket), socket.EndSend)
End Function

However, in the VB.NET syntax, I get the following error. This code worked perfectly in C#.

Overload resolution failed because no accessible 'EndSend' accepts this number of arguments.

Am I missing something?

Foi útil?

Solução

You have to use the AddressOf operator:

Public Shared Function SendAsync(socket As Socket, buffer As Byte(), offset As Integer, count As Integer) As Task(Of Integer)
    Return Task.Factory.FromAsync(...(...), AddressOf socket.EndSend)
End Function

Since you can omit the () when calling a method in VB.Net, the compiler does not know that you want to use socket.EndSend as a delegate instead of calling it and thus expects it to return a Func(Of IAsyncResult, Integer), which it doesn't do. Hence the overload resolution error.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top