1

I am trying to convert some C# code to VB .Net and am having issues with some of the delgate conversions.

BackupLibraries.cs

Public Class Backup
{
public event EventHandler<BackupPEventArgs> Backup;

...
if (BackupProgress != null) BackupProgress(this, new BackupProgressEventArgs(percent_complete))
...
}

public class BackupEventArgs : EventArgs
    {
        private float percentage;

        public BackupProgressEventArgs(float percentage)
        {
            this.percentage = percentage;
        }

        public float Percentage
        {
            get { return percentage; }
        }
    }

UnitTest.cs

public void BackupTest()
{
Backup bu = new Backup()
        bu.BackupProgress += delegate(object sender, Backup.BackupEventArgs e)

        {
            Debug.WriteLine("Percentage: " + e.Percentage.ToString());
        };
}

It's converting sb.Backup += delegate(object sender, SQLBackup.BackupProgressEventArgs e) and wiring it to the event in the other class I am having issues on.

In VB:

Backuplibraries.vb

Public Class Backup

Public Event Backup As EventHandler(Of BackupEventArgs)

...
RaiseEvent Backup(Me, New BackupEventArgs(percent_complete))
...
End Class

Public Class BackupEventArgs
            Inherits EventArgs

UnitTest.vb

Public Class BackupTest

Dim bu As New Backup()

bu.BackupProgress = Sub(sender As Object, e As BackupEventArgs)
                         Debug.WriteLine(e.Percentage.ToString())
                         End Sub

End Class

Of course

bu.BackupProgress = Sub(sender As Object, e As BackupEventArgs)
                             Debug.WriteLine(e.Percentage.ToString())
                             End Sub

is where the issue I am having with the conversion from C# to .Net is.

Jon B
  • 51,025
  • 31
  • 133
  • 161
PMOrion
  • 169
  • 2
  • 10
  • +1 Some related questions (`+=` -> `AddHandler`) but not exact duplicates due to the anonymous delegate in this question. They are probably duplicates of each other. http://stackoverflow.com/questions/7636382/how-can-i-use-the-following-events-delgates-written-in-c-in-vb-net http://stackoverflow.com/questions/9728926/how-to-convert-this-line-from-c-sharp-to-vb-net-windows-phone-7 http://stackoverflow.com/questions/3492674/can-you-convert-this-to-vb http://stackoverflow.com/questions/4448323/how-do-i-translate-newbutton-click-delegate-window-isopen-false-in-vb – MarkJ Nov 08 '12 at 16:25

1 Answers1

2

You might try AddHandler here

AddHandler bu.BackupProgress, AddressOf Sub(sender As Object, e As BackupEventArgs)
                         Debug.WriteLine(e.Percentage.ToString())
                         End Sub

To have the arguments come in, this would not be able to remain anonymous in this fashion in VB.Net. You would need to abstract out your method with a fixed address.

Protected Sub BackupProgressDelegate(sender as Object, e as BackupEventArgs)
    Debug.WriteLine(e.Percentage.ToString())
End Sub

...

AddHandler bu.BackupProgress, AddressOf BackupProgressDelegate
Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • Oh, you were faster. Deleted my answer since it was the same. – Dennis Traub Nov 08 '12 at 16:01
  • @DennisTraub: Speed at the cost of formatting I guess. Your answer was prettier :) – Joel Etherton Nov 08 '12 at 16:02
  • 2
    I'm pretty sure that the `AddressOf` should not be here. It's needed for method groups (e.g. `AddressOf MyMethod`), not for lambdas. – Heinzi Nov 08 '12 at 16:04
  • @Heinzi: This is not a lambda, it's an anonymous delegate. – Joel Etherton Nov 08 '12 at 16:08
  • @Heinzi: Actually I stand corrected. Adding parameters to the anonymous delegate creates this as a lambda, and even in 4.0 the compiler does not like the anonymous delegate as written. – Joel Etherton Nov 08 '12 at 16:17
  • @KonradRudolph: I created a new solution in 2010 and actually put the code in substituting button events for OPs custom events. The result ended up being that the compiler barked at me regarding the anonymous delegate as written. It also indicated that with arguments the compiler was analyzing it as a lambda expression. I was very sure I'd used the original syntax before, but can't remember the project and can't reproduce the results just now. – Joel Etherton Nov 08 '12 at 16:24
  • @Joel Weird. But either you made a mistake or this is a bug in the compiler, it’s unrelated to delegate-vs.-lambda and to presence or absence of arguments. The equivalent code works in C#. (`bu.BackupProgress += (sender, e) => {…}`). – Konrad Rudolph Nov 08 '12 at 16:26
  • @Joel No, it works [according to MSDN](http://msdn.microsoft.com/en-us/library/bb763088.aspx) and [according to Stack Overflow](http://stackoverflow.com/a/9689097/1968) – Konrad Rudolph Nov 08 '12 at 16:28
  • @KonradRudolph: That's a great article, but I must be missing something in my solution because that example code also generates compile errors for me. – Joel Etherton Nov 08 '12 at 16:41
  • @JoelEtherton: Are you sure? Your original solution without the `AddressOf` should work fine, and I actually use this syntax quite often. What compile error do you get? (PS: VB.NET does not support anonymous delegates, but it does support lambdas.) – Heinzi Nov 08 '12 at 21:44