将c#片段转换为vb .net(re EventHandler)

时间:2014-06-18 14:28:55

标签: c# .net vb.net events

我已经离开了一段时间,现在又做了一些编码。

我无法将特定的c#片段转换为vb .net。任何帮助将不胜感激:

public Form1()
{
    // Add a listener for usb events
    myUsbDevice.usbEvent += new usbDevice.usbEventsHandler(usbEvent_receiver);
}

private void usbEvent_receiver(object o, EventArgs e)
{
    // Do Stuff
}

相关位是:

// usbDevice Class        
public delegate void usbEventsHandler(object sender, EventArgs e);
public event usbEventsHandler usbEvent;

我的转换是:

// usbDevice Class
Public Delegate Sub usbEventsHandler(sender As Object, e As EventArgs)
Public Event usbEvent As usbEventsHandler

&安培;这显然不起作用(显然):

Private Sub Form1()
    myUsbDevice.usbEvent += New usbDevice.usbEventsHandler(AddressOf usbEvent_receiver)  // this doesn't work. Says to use RaiseEvent although I think it should just be an AddHandler
End Sub

Private Sub usbEvent_receiver(o As Object, e As EventArgs)
    // Do stuff
End Sub

我只是不太确定如何转换它。 只是使用AddHandler似乎没有意义,也不会:

AddHandler myUsbDevice.usbEvent, Sub usbEvent_receiver() // am missing 'o' object and 'e' eventargs + the whole usbDevice.usbEventsHandler bit is gone and I don't think/know whether that's right.

1 个答案:

答案 0 :(得分:1)

在这种情况下,c#中的+=分配了一个事件处理程序。

要在VB中执行此操作,它将如下所示:

AddHandler myUsbDevice.usbEvent, AddressOf usbEvent_receiver

传递o对象和e eventargs而不必明确命名它们。

您已经正确设置了usbEvent_receiver的签名。 VB将完成其余的工作以传递参数。