3

I have existing VB GUI code and I am trying to interface with some C# code but can't work out how to pass the EventHandler to VB.NET.

The C# signature is:

public void SendLatestImages(Guid PatientID, Guid ToPracticeID, EventHandler<ProgressChangedEventArgs> progressChangedHandler)
    {
        ...
    }

In VB when I try to consume this, I have

sendImages.SendLatestImages(arg.PatientID, arg.ToPracticeID, ProgressStream_ProgressChanged)

So far so good. But in the ProgressStream_ProgressChanged function I only get:

Private Function ProgressStream_ProgressChanged() As EventHandler(Of SLSyncClient.ProgressChangedEventArgs)

End Function

... There is no access to the actual ProgressChangedEventArgs that I am after. In C#, the signature on this last function is

private void ProgressStream_ProgressChanged(object sender, ProgressChangedEventArgs e)

... which gives me the args as e. What am I missing here?

1 Answer 1

2

Your problem is that ProgressStream_ProgressChanged is a method that returns an event handler, not a method that is the event handler itself. What your code does is that it invokes ProgressStream_ProgressChanged (you don't need parentheses for that in VB) and passes its result to SendLatestImages.

What you want to do is to make ProgressStream_ProgressChanged into a Sub that matches the EventHandler delegate (and not a Function that returns it):

Private Sub ProgressStream_ProgressChanged(sender As Object, args As ProgressChangedEventArgs)
End Sub

Then you can use AddressOf to create a delegate out of it (in C# you don't need any operator for that, in VB you do):

sendImages.SendLatestImages(arg.PatientID, arg.ToPracticeID, AddressOf ProgressStream_ProgressChanged)
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. I was missing the AddressOf. (I had tried the sub with the correct signature). Thank you @svick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.