I need help converting some vb.net code to C#.net code

JDL129 used Ask the Experts™
I need to change this to C# and I need to know where to put it.      

Public WithEvents UPED As New SIM.Charge
Is this the correct translation?
SIM.Charge UPED = new SIM.Charge();

When I run the code I get the error message below but the SIM.dll is in the path specified by the properties of the dll.

System.IO.FileNotFoundException was unhandled
  Message="Could not load file or assembly 'Hid.Net, Version=, Culture=neutral, PublicKeyToken=784515207d9cd9fa' or one of its dependencies. The system cannot find the file specified."
  FileName="Hid.Net, Version=, Culture=neutral, PublicKeyToken=784515207d9cd9fa"
  FusionLog="=== Pre-bind state information ===\r\nLOG: User = CAKPOSSERVER\\Jerry\r\nLOG: DisplayName = Hid.Net, Version=, Culture=neutral, PublicKeyToken=784515207d9cd9fa\n (Fully-specified)\r\nLOG: Appbase = file:///C:/POSiTrackC19/POSiTrackC/POSiTrackC/bin/Debug/\r\nLOG: Initial PrivatePath = NULL\r\nCalling assembly : SIM, Version=, Culture=neutral, PublicKeyToken=5456cc89cbcb6145.\r\n===\r\nLOG: This bind starts in default load context.\r\nLOG: Using application configuration file: C:\\POSiTrackC19\\POSiTrackC\\POSiTrackC\\bin\\Debug\\POSiTrackC.vshost.exe.Config\r\nLOG: Using machine configuration file from C:\\Windows\\Microsoft.NET\\Framework\\v2.0.50727\\config\\machine.config.\r\nLOG: Post-policy reference: Hid.Net, Version=, Culture=neutral, PublicKeyToken=784515207d9cd9fa\r\nLOG: Attempting download of new URL file:///C:/POSiTrackC19/POSiTrackC/POSiTrackC/bin/Debug/Hid.Net.DLL.\r\nLOG: Attempting download of new URL file:///C:/POSiTrackC19/POSiTrackC/POSiTrackC/bin/Debug/Hid.Net/Hid.Net.DLL.\r\nLOG: Attempting download of new URL file:///C:/POSiTrackC19/POSiTrackC/POSiTrackC/bin/Debug/Hid.Net.EXE.\r\nLOG: Attempting download of new URL file:///C:/POSiTrackC19/POSiTrackC/POSiTrackC/bin/Debug/Hid.Net/Hid.Net.EXE.\r\n"
       at SIM.USB_HID_Driver.USBInterface..ctor()
       at SIM.USB_HID..ctor()
       at SIM.Charge..ctor()
       at POSiTrackC.frmCCMX..ctor() in C:\POSiTrackC19\POSiTrackC\POSiTrackC\frmCCMX.cs:line 15
       at POSiTrackC.frmEntry.btnCreditCard_Click(Object sender, EventArgs e) in C:\POSiTrackC19\POSiTrackC\POSiTrackC\frmEntry.cs:line 1020
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at POSiTrackC.Program.Main() in C:\POSiTrackC19\POSiTrackC\POSiTrackC\Program.cs:line 27
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Todd GerbertIT Consultant
Top Expert 2010

To convert from VB.Net to C# just put a semi-colon at the end of every line! ;)

Your C# code to declare and instantiate the SIM.Charge object is correct. That error message would suggest your project is missing a reference - right click on your project, choose Add Reference, and add the DLL containing SIM.Charge to your project.



 Thanks for the response unfortunately neither suggestion worked.:(  Maybe I just don't know where to place them in the code.

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Unfortunately, there is NO direct C# equivalent for the VB.Net WithEvents mechanism.

In VB.Net, you can subscribe to an event for a variable declared as "WithEvents" by tagging a sub with a "Handles" clause.

To convert that to C# you would need to manually "wire up" the event to your desired method.  This can be done either thru the IDE at design-time, or at run-time.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Todd GerbertIT Consultant
Top Expert 2010

I was joking about the semi-colon thing, "SIM.Charge UPED = new SIM.Charge();" is correct. ;)

The error message reads Could not load file or assembly 'Hid.Net..., which means either Hid.Net couldn't be loaded, or some other DLL that Hid.Net itself uses couldn't be loaded. Do you have a Hid.Net.Dll? Did you add it as a reference to your project?
Todd GerbertIT Consultant
Top Expert 2010

Subscribing to events won't do any good until we've got all the necessary assembly references. :P
Please post the complete code here So that I can help you out better.. :)


guys!!  Thanks for your posts!!!

Attached is the code that I am trying to run.  As you can see I have the SIM.dll in the using section and also have it in the references.

I am a very newbie in .Net.  

This is another line of code that needs to be translated:
UPED = New SIM.Charge(cboPort.Text, Baud, Parity, databits, cboDevice.SelectedIndex, txtLogLevel.Text)
 Where and how would declare UPED?

Thanks guys!!!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SIM;

namespace POSiTrackC
    public partial class frmCCMX : Form
        SIM.Charge UPED = new SIM.Charge();

        public frmCCMX()


        private void btnRequestSwipe_Click(object sender, EventArgs e)


        private void btnProcess_Click(object sender, EventArgs e)


        private void button1_Click(object sender, EventArgs e)

            UPED.MxCommSetting = SIM.Charge.MxCommType.Serial;
            UPED.Baud = "115200";
            UPED.DataBits = "8";
            UPED.Parity = "E";
            UPED.ComPort = 5;
            UPED.Timeout = 10000;
            UPED.Device = SIM.Charge.DeviceType.ppdVerifone_Mx870;

        Select Case UPED.InitalizePinPad;{
            Case True;
                MessageBox.show("PinPad Initialized");
                lblPPStatus.Text = "PinPad initialized successfully";;
            Case Else;
                MessageBox.show("PinPad Initialize Failure");
                lblPPStatus.Text = "PinPad initialize failure";
        End Select;

Open in new window

.Net Full Stack Developer


Dim WithEvents MyButton As Button   ' WithEvents can't be used on local variable
MyButton = New Button

Private Sub MyButton_Click(ByVal sender As System.Object, _
  ByVal e As System.EventArgs) Handles MyButton.Click
  MessageBox.Show(Me, "Button was clicked", "Info", _
    MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub


Button MyButton = new Button();
MyButton.Click += new System.EventHandler(MyButton_Click);

private void MyButton_Click(object sender, System.EventArgs e) {
  MessageBox.Show(this, "Button was clicked", "Info",
    MessageBoxButtons.OK, MessageBoxIcon.Information);

For Translatoin Check this out :




THANKS FOR THE LINKS!!!  If this is from Harding University, I went there in 64 and 65.

Thanks again,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial