Avatar of JDL129

asked on 

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

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()

C#.NET Programming

Avatar of undefined
Last Comment
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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.
Avatar of JDL129



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

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.
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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?
Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

Subscribing to events won't do any good until we've got all the necessary assembly references. :P
Avatar of starlite551
Flag of India image

Please post the complete code here So that I can help you out better.. :)
Avatar of JDL129


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

Avatar of Navneet Hegde
Navneet Hegde
Flag of United States of America image

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of JDL129


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

Thanks again,
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo