Results 1 to 10 of 10

Thread: Converting C# to VB.net

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Converting C# to VB.net

    How can I convert these C# lines into VB.net:

    Code:
    usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
    usbDevices.DeviceAttached += new EventHandler(usbDevices_DeviceAttached);
    usbDevices.DeviceRemoved += new EventHandler(usbDevices_DeviceRemoved);
    Is there a += operator in VB.net?

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Converting C# to VB.net

    Code:
    usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB)
    AddHandler usbDevices.DeviceAttached, New EventHandler(AddressOf usbDevices_DeviceAttached)
    AddHandler usbDevices.DeviceRemoved, New EventHandler(AddressOf usbDevices_DeviceRemoved)

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting C# to VB.net

    I Receive an error with the above code indicating that usbDevices_DeviceAttached and usbDevices_DeviceRemoved are undeclared. These two names are supposed to be the names of the handlers that are being created. They do not have any values.

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Converting C# to VB.net

    There must be something like

    Code:
    private void usbDevices_DeviceAttached(...
    {
      ...
    }
    in the c# code you're converting. That's the event handler for the DeviceAttached event.
    You should add this sub to your code in order for this to work:

    Code:
    Private Sub usbDevices_DeviceAttached(...
    
    End Sub

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting C# to VB.net

    I tried this instead

    <code> AddHandler usbDevices_DeviceAttached, AddressOf usbDevices.DeviceAttached
    AddHandler usbDevices_DeviceRemoved, AddressOf usbDevices.DeviceRemoved
    </Code>

    but I receive the error: "AddressOf operand must be the name of a method(without parenthesis)"

    usbDevices.DeviceAttached() and usbDevices.DeviceRemoved() have the following signature:

    (sender as object, e as system.eventargs)

    These are functions that are being invoked from a dll:

    imports CyUSB

    Dim usbDevices As USBDeviceList 'USB device list
    Dim myDevice As CyUSBDevice

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting C# to VB.net

    Actually, I think you may be right. The function needs to be defined. here is an example:

    Code:
    void usbDevices_DeviceAttached(object sender, EventArgs e)
    {
    USBEventArgs usbEvent = e as USBEventArgs;
    // Take some action
    }
    I can define this function but I don't know what USBEventArgs is. It is not defined in the dll.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: Converting C# to VB.net

    Load the working C# application in the VS IDE, press F2 to bring up the object browser and use that to search for USBEventArgs. You would be able to trace it from there to the namespace and even the assembly it belongs to.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Converting C# to VB.net

    The USBeventArgs class should be defined in the same dll that your USBDeviceList class.
    Just browse through this dll using the object browser.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2012
    Posts
    27

    Re: Converting C# to VB.net

    Thanks for the assistance. What is the VB equivalent of these lines:

    Code:
    USBDeviceList usbDevices = new USBDeviceList(CyConst.DEVICES_CYUSB);
    CyUSBDevice StreamDevice = usbDevices["Cy Stream Device"] as CyUSBDevice;
    The first line I am able to convert just fine: (usbDevices is already declared)
    Code:
    usbDevices = New USBDeviceList(CyConst.DEVICES_CYUSB)
    I am not certain of how to create a new instance of streamDevice as a CyUSBDevice: (I already declared StreamDevice as a CyUSBDevice)
    Code:
    StreamDevice = usbDevices("Cy Stream Device") (as CyUSBDevice)
    Last edited by mk48; Apr 4th, 2012 at 12:07 AM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Converting C# to VB.net

    csharp Code:
    1. CyUSBDevice StreamDevice = usbDevices["Cy Stream Device"] as CyUSBDevice;
    vb.net Code:
    1. Dim StreamDevice As CyUSBDevice = TryCast(usbDevices("Cy Stream Device"), CyUSBDevice)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width