0

I've been out of the loop for a while and now doing some coding again.

I'm having trouble convert a particular c# snippet to vb .net. Any help would be appreciated:

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
}

Relevant bits are:

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

My conversion is this:

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

& this this what doesn't work (obviously):

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

I'm just not quite sure how to convert it. Just using AddHandler doesn't seem to make sense and won't either:

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 Answer 1

1

The += in c# in this case is assigning an event handler.

To do this in VB it would look like:

AddHandler myUsbDevice.usbEvent, AddressOf usbEvent_receiver

The o of object and e of eventargs are passed without having to name them explicitly.

You have already set up the signature properly of usbEvent_receiver. VB will do the rest to pass the parameters.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.