Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax for adding an event handler in VB.NET

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);
like image 300
sharkyenergy Avatar asked Jul 07 '13 10:07

sharkyenergy


People also ask

How do you call an event handler in VB net?

Call an event handler using AddHandler Make sure the event is declared with an Event statement. Execute an AddHandler statement to dynamically connect the event-handling Sub procedure with the event. When the event occurs, Visual Basic automatically calls the Sub procedure.

How do you create event handlers with visual studio net?

From the Class Name drop-down list at the top of the Code Editor, select the object that you want to create an event handler for. From the Method Name drop-down list at the top of the Code Editor, select the event. Visual Studio creates the event handler and moves the insertion point to the newly created event handler.

What is an event how event handler is added?

Event handler code can be made to run when an event is triggered by assigning it to the target element's corresponding onevent property, or by registering the handler as a listener for the element using the addEventListener() method.


1 Answers

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!

like image 132
Cody Gray Avatar answered Sep 21 '22 13:09

Cody Gray