RichieRich
RichieRich

Reputation: 197

Passing EventHandler as argument from C# to VB.NET

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?

Upvotes: 3

Views: 1091

Answers (1)

svick
svick

Reputation: 244777

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)

Upvotes: 2

Related Questions