Converting C# to VB.net but code gives null reference
-
Hi Please forgive me, this may be a simple answer for some. I have self taught myself VB.net and have developed some software for myself. I often get code samples for hardware that i use, but it is in C#. I have used code converters, but I still seem to get issues. The major issue I have is below. The C# code works perfectly, but the VB code gives me a "Object reference not set to an instance of an object.'" error when I call the connect sub. Please help. C# Code
using SPORTident;
using System.Windows.Forms;
using SPORTident.Communication;
using System;namespace SportidentMinimal
{
public partial class MainForm : Form
{
public Reader reader;public MainForm() { InitializeComponent(); reader = new Reader { WriteBackupFile = true, BackupFileName = System.IO.Path.Combine(Environment.CurrentDirectory, $@"backup\\{DateTime.Now:yyyy-MM-dd}\_stamps.bak") }; reader.DeviceConfigurationRead += new DeviceConfigurationReadEventHandler(reader\_DeviceConfigurationRead); reader.InputDeviceChanged += new ReaderDeviceChangedEventHandler(reader\_InputDeviceChanged); reader.InputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader\_InputDeviceStateChanged); reader.CardRead += new SPORTident.DataReadCompletedEventHandler(reader\_CardRead); } private void reader\_DeviceConfigurationRead(object sender, StationConfigurationEventArgs e) { writeLog("StationConfigRead, OperatingMode: " + e.Device.OperatingMode); writeLog("StationConfigRead, LegacyProtocolMode: " + e.Device.LegacyProtocolMode); writeLog("StationConfigRead, AutoSend: " + e.Device.AutoSendMode); writeLog("StationConfigRead, FirmwareVersion: " + e.Device.FirmwareVersion); writeLog("StationConfigRead, CodeNumber: " + e.Device.CodeNumber); writeLog("StationConfigRead, SerialNumber: " + e.Device.SerialNumber); } private void reader\_InputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e) { writeLog("InputDeviceStateChanged: " + e.PreviousState + " => " + e.CurrentState); } private void reader\_InputDeviceChanged(object sender, ReaderDeviceChangedEventArgs e) { writeLog("InputDeviceChanged: " + e.PreviousDevice + " => " + e.CurrentDevice); } private
-
Hi Please forgive me, this may be a simple answer for some. I have self taught myself VB.net and have developed some software for myself. I often get code samples for hardware that i use, but it is in C#. I have used code converters, but I still seem to get issues. The major issue I have is below. The C# code works perfectly, but the VB code gives me a "Object reference not set to an instance of an object.'" error when I call the connect sub. Please help. C# Code
using SPORTident;
using System.Windows.Forms;
using SPORTident.Communication;
using System;namespace SportidentMinimal
{
public partial class MainForm : Form
{
public Reader reader;public MainForm() { InitializeComponent(); reader = new Reader { WriteBackupFile = true, BackupFileName = System.IO.Path.Combine(Environment.CurrentDirectory, $@"backup\\{DateTime.Now:yyyy-MM-dd}\_stamps.bak") }; reader.DeviceConfigurationRead += new DeviceConfigurationReadEventHandler(reader\_DeviceConfigurationRead); reader.InputDeviceChanged += new ReaderDeviceChangedEventHandler(reader\_InputDeviceChanged); reader.InputDeviceStateChanged += new ReaderDeviceStateChangedEventHandler(reader\_InputDeviceStateChanged); reader.CardRead += new SPORTident.DataReadCompletedEventHandler(reader\_CardRead); } private void reader\_DeviceConfigurationRead(object sender, StationConfigurationEventArgs e) { writeLog("StationConfigRead, OperatingMode: " + e.Device.OperatingMode); writeLog("StationConfigRead, LegacyProtocolMode: " + e.Device.LegacyProtocolMode); writeLog("StationConfigRead, AutoSend: " + e.Device.AutoSendMode); writeLog("StationConfigRead, FirmwareVersion: " + e.Device.FirmwareVersion); writeLog("StationConfigRead, CodeNumber: " + e.Device.CodeNumber); writeLog("StationConfigRead, SerialNumber: " + e.Device.SerialNumber); } private void reader\_InputDeviceStateChanged(object sender, ReaderDeviceStateChangedEventArgs e) { writeLog("InputDeviceStateChanged: " + e.PreviousState + " => " + e.CurrentState); } private void reader\_InputDeviceChanged(object sender, ReaderDeviceChangedEventArgs e) { writeLog("InputDeviceChanged: " + e.PreviousDevice + " => " + e.CurrentDevice); } private
Do you have an Item selected inside the Combobox (cbbSportIdentDevices.SelectedItem) ? What do you see by using the Debugger (in case of exeption) ?
-
Do you have an Item selected inside the Combobox (cbbSportIdentDevices.SelectedItem) ? What do you see by using the Debugger (in case of exeption) ?
Hi Ralf Yes I select a Com port. For my current scenario it happens to be COM5. Its weird that it all works perfectly in C#, but in my project, when I have converted to VB, it gives this exception.
-
Hi Ralf Yes I select a Com port. For my current scenario it happens to be COM5. Its weird that it all works perfectly in C#, but in my project, when I have converted to VB, it gives this exception.
Hi Shane, I never thought that you assign a Com-Port at this place. So ... there are some Information missing to go further. How is ReaderDeviceInfo declared ? How and where do you have the COM-Port in your code ? Where do you assign an initialize it ? I think that are parts which belong to the Designer-Scripts from the C#-Project - you should look also there and perhaps complete your code. If not try to answer my questions ...
-
Hi Shane, I never thought that you assign a Com-Port at this place. So ... there are some Information missing to go further. How is ReaderDeviceInfo declared ? How and where do you have the COM-Port in your code ? Where do you assign an initialize it ? I think that are parts which belong to the Designer-Scripts from the C#-Project - you should look also there and perhaps complete your code. If not try to answer my questions ...
Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane
-
Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane
Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf
-
Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf
Yes, all dll's have been added. Very confusing to me, why it doesnt work
-
Hi Shane, when I compare both codes (C# vs. VB) I can't see any relevant difference. For me both codes should do the same. But any difference must be there ... So : what about the Visual Studio Settings ? Have you added the required DDL's to the references (I hope the name is right - In German it's called 'Verweis') of your Project ? Greetings Ralf
If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything
Private Sub connect() reader = New Reader() Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo) reader.InputDevice = readerDeviceInfo reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None) reader.OpenInputDevice() reader.OpenOutputDevice() End Sub
-
If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything
Private Sub connect() reader = New Reader() Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo) reader.InputDevice = readerDeviceInfo reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None) reader.OpenInputDevice() reader.OpenOutputDevice() End Sub
Let's get back to the ComboBox and it's ReaderDeviceInfo ... I can't see a code-line where the ComboBox is assigned/filled AND with what it is filled. I suggest that you should look there ...
-
If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything
Private Sub connect() reader = New Reader() Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo) reader.InputDevice = readerDeviceInfo reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None) reader.OpenInputDevice() reader.OpenOutputDevice() End Sub
Shane James wrote:
If I put the reader = New Reader() in the connect sub, the error goes away, but nothing works like it should. it doesnt pick up the events or anything
This is normal: you create a new instance of the Reader class but this one is not configured like the first one was in the constructor of
Results_Capture_Enduro
. But I don't think the problem is here, you should remove this line. You did not tell us which variable triggers the exception. To find it you can remove the try...catch block around the call to connect() to let the debugger stop at the line the exception occurs. There is one line where the VB code is not equivalent to its C# counterpart:Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
The strictly equivalent version would be:
Dim readerDeviceInfo As ReaderDeviceInfo = DirectCast((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
See this article: DirectCast vs. CType[^] for explanations. In general using CType for a simple type cast is dangerous because it will try to do something even if the cast is not possible, so that the error may propagate to other parts of the code where it is difficult to understand. NB. Also be careful when translating from C# to VB.NET as the latter is not really case sensitive which may cause compilation errors: vb.net - Is VB really case insensitive? - Stack Overflow[^]
-
Perhaps little background will help as I can't post an image of the form. I have an RFID reader which connects to the computer via a usb. The sample app from the supplier (the c# Code). Just provides a list of available devices in a combobox after clicking the refresh button. This all works in my VB code as well and after clicking the button it shows a list of (COM1 and COM5 on my laptop) in the combobox. After selecting the correct COM from the dropdown (COM5 in my case). You then click the connect button and it is at this point that my VB code falls over, but the C# code works fine. The form is very simple, I have compared the designer code and nothing different there either. I'm just very confused as to why the VB code gives the null exception but the C# code doesn't. Yet the code is almost identical. The writelog is just a list box which provides a log of all the steps taken and events which are initiated by the reader. Hope this helps Regards Shane
Shane James wrote:
it shows a list of (COM1 and COM5 on my laptop) in the combobox.
I am going to guess that it does not contain a list of strings that represent comports, but objects. Show us how the VB version that initializes the combobox looks like.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)