16

I have following code i need to convert to VB.NET. Problem is every translation tool I found is converting the add handler part wrong. I don't seem to be able to do it by myself.

FtpClient ftpClient = new FtpClient();
ftpClient.UploadProgressChanged += new EventHandler<UploadProgressChangedLibArgs>(ftpClient_UploadProgressChanged);
ftpClient.UploadFileCompleted += new EventHandler<UploadFileCompletedEventLibArgs>(ftpClient_UploadFileCompleted);
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
sharkyenergy
  • 3,842
  • 10
  • 46
  • 97

1 Answers1

30

There are two different ways to associate event handler methods with an event in VB.NET.

The first involves the use of the Handles keyword, which you append to the end of the event handler method's definition. For example:

Sub ftpClient_UploadProgressChanged(sender As Object, e As UploadProgressChangedLibArgs) Handles ftpClient.UploadProgressChanged
    ' ...
End Sub

Sub ftpClient_UploadFileCompleted(sender As Object, e As UploadFileCompletedEventLibArgs) Handles ftpClient.UploadFileCompleted
    ' ...
End Sub

The first method is much simpler if you've already got separately defined event handler methods anyway (i.e., if you're not using a lambda syntax). I would recommend it whenever possible.

The second involves the explicit use of the AddHandler statement, just like += in C#. This is the one you need to use if you want to associate event handlers dynamically, e.g. if you need to change them at run time. So your code, literally converted, would look like this:

Dim ftpClient As New FtpClient()
AddHandler ftpClient.UploadProgressChanged, AddressOf ftpClient_UploadProgressChanged
AddHandler ftpClient.UploadFileCompleted, AddressOf ftpClient_UploadFileCompleted

Like you said, I tried running your code through Developer Fusion's converter and was surprised to see that they were returning invalid VB.NET code:

' WRONG CODE!
Dim ftpClient As New FtpClient()
ftpClient.UploadProgressChanged += New EventHandler(Of UploadProgressChangedLibArgs)(ftpClient_UploadProgressChanged)
ftpClient.UploadFileCompleted += New EventHandler(Of UploadFileCompletedEventLibArgs)(ftpClient_UploadFileCompleted)

Turns out, that's a known bug that might be worth voting for!

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • i am getting this error now, what does this mean? thanks `Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.` – sharkyenergy Jul 07 '13 at 14:07
  • 1
    @Justme Use the Handles keyword at the end of a procedure declaration to cause it to handle events raised by an object variable declared using the **WithEvents** keyword. http://msdn.microsoft.com/library/vstudio/6k46st1y – Chris Jul 07 '13 at 15:31
  • thanks for the answer! now its working.. i have another problem tough.. since the uploader is working on another thread, also the sub called by the event is executed on another thread. can i force that one to be run on the main thread? i need it to access the interface.. – sharkyenergy Jul 07 '13 at 16:14
  • @Just To run code on the UI thread or interact with controls, use the `Invoke` method. Lots of information about this here already, for example [this question](http://stackoverflow.com/questions/22356/cleanest-way-to-invoke-cross-thread-events). – Cody Gray - on strike Jul 08 '13 at 05:11
  • and how would ` Public Event UploadProgressChanged As EventHandler(Of UploadProgressChangedLibArgs)` be set to a correct sentence? online its still covnerting wrong.. thanks – sharkyenergy Jul 27 '13 at 09:40