2

I have a problem with coverting c# to vb.net on using DispatcherOperationCallback.

I have tried to convert it based on help to convert c# anonymous to vb.net

I'm using VS2010.

I have a c# code like this:

    public void Callback(Contract contract)
    {
        Dispatcher.BeginInvoke(DispatcherPriority.Normal,
              (DispatcherOperationCallback)delegate(object arg)
              {
                  Contract obj = (Contract)arg;
                  txtRequest.Text = HandleArgument(obj);                      
                  return null;
              }, contract);
    }

And After I tried to change to vb.net like this

Public Sub Callback(ByVal contract As Contract) Implements IServiceCallback.Callback

    Dispatcher.BeginInvoke(New DispatcherOperationCallback(Sub(arg As Object)
                                                           txtRequest.Text = HandleArgument(DirectCast(arg, Contract))
                                                           End Sub), DispatcherPriority.Normal, contract)

End Sub

but it did not work. The vs2010 displayed "nested sub does not have a signature that is compatible with delegate "Delegate Function DispatcherOperationCallback(arg As Object) As Object"

Thank you for your help.

Community
  • 1
  • 1
tong
  • 259
  • 5
  • 23

1 Answers1

2

Just read the error message:

nested sub does not have a signature that is compatible with delegate 
  "Delegate Function DispatcherOperationCallback(arg As Object) As Object

so, you should change you code accordingly.

Public Sub Callback(ByVal contract As Contract) Implements IServiceCallback.Callback
    Dispatcher.BeginInvoke(
      New DispatcherOperationCallback(
          Function(arg As Object)
              txtRequest.Text = HandleArgument(DirectCast(arg, Contract))
              Return Nothing ' // you need to check what should be returned here '
          End Function), 
         DispatcherPriority.Normal, 
         contract)
End Sub
Paulo Santos
  • 11,285
  • 4
  • 39
  • 65
  • In fact, I do not need to return anything. I just want to pass arg to HandleArgument function to perform some task and return string back to TextBox named txtRequest. That's all. – tong Mar 27 '11 at 09:52
  • That may be true, but the `DispatcherOperationCallback` expects a function, not a method. – Paulo Santos Mar 27 '11 at 09:56