2

This code is part of AjaxControlToolkitSampleSite. To be exact, it is in the AsyncFileUpload control:

 AsyncFileUpload1.UploadedComplete += new EventHandler<AsyncFileUploadEventArgs>(AsyncFileUpload1_UploadedComplete);

How can I translate this to VB.NET?

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
  • 5
    http://www.developerfusion.com/tools/convert/csharp-to-vb/ – Rune FS Jan 12 '12 at 19:21
  • 2
    @rune That converter generates **invalid code** in this case. It is not aware of VB.Net AddHandler. As has been reported on the duplicate questions. – MarkJ Jan 12 '12 at 21:39
  • @MarkJ yeah they are not perfect but usually a pretty good starting point and god forbid that you'll actually have to do some manual work :). Jokes aside your comment is the reason why I posted it as a comment and not part of an answer – Rune FS Jan 13 '12 at 09:48
  • 2
    @RuneFS But if you only post the link with no explanation, most people assume you mean that **the converter can automatically solve the original question**. I expect that's the assumption made by the five people who have now upvoted your comment. Whereas in fact for this question the converter is really so useless it's almost embarrassing – MarkJ Jan 13 '12 at 10:42
  • @MarkJ I wouldn't call it use less. That converter helped me solve this problem when I had to convert from C# to V.NEt but then again I only expected it to get me started (and I had more than one line of code which I'm sure OP has too) – Rune FS Jan 13 '12 at 12:38

4 Answers4

7

Here you go:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

Alternatively, within your code, you can select the AsyncFileUpload1 control from the left-hand dropdown list (just above the code) and then select the UploadComplete event from the right-hand dropdown list.

This will automatically create an event handler with the correct signature using the VB Handles declaration.

competent_tech
  • 44,465
  • 11
  • 90
  • 113
  • 1
    You are welcome. Remember, when an answer to a question solves or helps you solve your question, you should click on the checkmark next to the answer so that future visitors to the question know that this is the answer that solved your question. Thanks! – competent_tech Jan 12 '12 at 19:29
1

Others have shown how to literally translate event+= to AddHandler in VB.

However, despite the similarities, VB and C# are different languages, and good C# code might not be good VB code when translated literally. For example, in VB, the canonical way to attach a fixed event handler to an ASP.NET control is by using the Handles keyword:

Protected Sub AsyncFileUpload1_UploadedComplete(sender As Object, _
                                                e As AsyncFileUploadEventArgs) _
    Handles AsyncFileUpload1.UploadedComplete

    ' Your event handler code is here

End Sub
Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

If you can put that code in a C# project that compiles, you can convert that project to VB.NET with SharpDevelop. This is probably the best way to translate between C# and VB.NET.

Also, ILSpy can translate a compiled dll written in C# into VB.NET

Justin Dearing
  • 14,270
  • 22
  • 88
  • 161
0

Two ways to do this:

If your AsyncFileUpload1 variable has the WithEvents qualifier, you can do the following using the Handles keyword on the event handler itself:

Private Sub AsyncFileUpload1_UploadedComplete(ByVal sender As Object, ByVal e As AsyncFileUploadEventArgs) Handles AsyncFileUpdate1.UploadedComplete

    'handler logic...

End Sub

If there is no WithEvents qualifier, then the following works:

AddHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

To remove the event handler, do the following:

RemoveHandler AsyncFileUpload1.UploadedComplete, AddressOf AsyncFileUpload1_UploadedComplete

Beware of the WithEvents/Handles route as this can cause memory leaks. It is simply syntactic sugar and wires up an AddHandler behind the scenes. I add this because I've been burned before with it while learning VB (I had a C# background).

Community
  • 1
  • 1
avanek
  • 1,649
  • 12
  • 20
  • `AddHandler` or C# `+=` is just as [vulnerable to memory leaks](http://stackoverflow.com/a/621177/15639) as `WithEvents`. It's exactly the same problem. Not a reason for avoiding `WithEvents` IMHO. – MarkJ Jan 13 '12 at 17:34
  • Right, I didn't say it wasn't vunerable. I just pointed out that `WithEvents` is essentially an `AddHandler` without a `RemoveHandler`, so it is prone to memory leaks just as `AddHandler` or `+=` is. I know when I code in C# and I have a memory leak, I start counting `+=`'s and `-=`'s for events. `WithEvents` can make this check a little more complicated in VB. – avanek Jan 13 '12 at 19:13