Click here to Skip to main content
15,895,529 members
Home / Discussions / Visual Basic
   

Visual Basic

 
Questionhelp me Pin
ago248621-Jul-17 9:52
ago248621-Jul-17 9:52 
AnswerRe: help me Pin
Afzaal Ahmad Zeeshan21-Jul-17 11:36
professionalAfzaal Ahmad Zeeshan21-Jul-17 11:36 
GeneralRe: help me Pin
ago248622-Jul-17 1:36
ago248622-Jul-17 1:36 
GeneralRe: help me Pin
Richard MacCutchan22-Jul-17 2:44
mveRichard MacCutchan22-Jul-17 2:44 
GeneralRe: help me Pin
Eddy Vluggen23-Jul-17 2:58
professionalEddy Vluggen23-Jul-17 2:58 
GeneralRe: Pin
ago248623-Jul-17 4:31
ago248623-Jul-17 4:31 
GeneralRe: Pin
Eddy Vluggen23-Jul-17 8:16
professionalEddy Vluggen23-Jul-17 8:16 
QuestionConverting C# to VB.net but code gives null reference Pin
Shane James21-Jul-17 0:56
Shane James21-Jul-17 0:56 
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 void reader_CardRead(object sender, SportidentDataEventArgs e)
        {
            SportidentCard card = e.Cards[0];
            writeLog("Card read: " + card.Siid);
        }

        private void button_refresh_Click(object sender, EventArgs e)
        {
            writeLog("Refresh pressed");
            clearSIDevices();
            getSIDevices();
        }

        private void button_sportidentConnect_Click(object sender, EventArgs e)
        {
            writeLog("Connect pressed");
            try
            {
                connect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button_sportidentDisconnect_Click(object sender, EventArgs e)
        {
            writeLog("Disconnect pressed");
            disconnect();
        }

        private void writeLog(string text)
        {
            if (listBox_log.InvokeRequired)
            {
                listBox_log.Invoke(new Action(() => writeLog(text)));
            }
            else
            {
                listBox_log.Items.Add(text);
                listBox_log.SelectedIndex = listBox_log.Items.Count - 1;
                listBox_log.SelectedIndex = -1;
            }
        }

        private void clearSIDevices()
        {
            writeLog("Clearing list of devices");
            comboBox_sportidentDevices.Items.Clear();
        }

        private void getSIDevices()
        {
            writeLog("Scanning for available devices");
            ReaderDeviceInfo.GetAvailableDeviceList();
            foreach (ReaderDeviceInfo device in ReaderDeviceInfo.AvailableDevices)
            {
                comboBox_sportidentDevices.Items.Add(device);
                writeLog("Adding device: " + device.DeviceName);
            }
            if (comboBox_sportidentDevices.Items.Count > 0)
            {
                writeLog("Found " + comboBox_sportidentDevices.Items.Count + " devices");
                comboBox_sportidentDevices.SelectedIndex = 0;
            }
            else
            {
                writeLog("No devices found");
            }
        }

        private void connect()
        {
            ReaderDeviceInfo readerDeviceInfo = (ReaderDeviceInfo)(comboBox_sportidentDevices.SelectedItem);
            reader.InputDevice = readerDeviceInfo;
            reader.OutputDevice = new ReaderDeviceInfo(ReaderDeviceType.None);
            reader.OpenInputDevice();
            reader.OpenOutputDevice();

            comboBox_sportidentDevices.Enabled = false;
            button_refresh.Enabled = false;
            button_sportidentConnect.Enabled = false;
            button_sportidentDisconnect.Enabled = true;
        }

        private void disconnect()
        {
            comboBox_sportidentDevices.Enabled = true;
            button_refresh.Enabled = true;
            button_sportidentConnect.Enabled = true;
            button_sportidentDisconnect.Enabled = false;

            reader.CloseInputDevice();
        }
    }
}


VB.NET Code
Imports SPORTident
Imports SPORTident.Communication


Public Class Results_Capture_Enduro
    Public reader As Reader


    Public Sub New()
        InitializeComponent()

        reader = New Reader() With {
                .WriteBackupFile = True,
                .BackupFileName = System.IO.Path.Combine(Environment.CurrentDirectory, "backup\{DateTime.Now:yyyy-MM-dd}_stamps.bak")
            }

        AddHandler reader.DeviceConfigurationRead, New DeviceConfigurationReadEventHandler(AddressOf reader_DeviceConfigurationRead)
        AddHandler reader.InputDeviceChanged, New ReaderDeviceChangedEventHandler(AddressOf reader_InputDeviceChanged)
        AddHandler reader.InputDeviceStateChanged, New ReaderDeviceStateChangedEventHandler(AddressOf reader_InputDeviceStateChanged)
        AddHandler reader.CardRead, New SPORTident.DataReadCompletedEventHandler(AddressOf reader_CardRead)

    End Sub

    Private Sub reader_DeviceConfigurationRead(sender As Object, e As StationConfigurationEventArgs)
        writeLog("StationConfigRead, OperatingMode: " & e.Device.OperatingMode.ToString)
        writeLog("StationConfigRead, LegacyProtocolMode: " & e.Device.LegacyProtocolMode.ToString)
        writeLog("StationConfigRead, AutoSend: " & e.Device.AutoSendMode.ToString)
        writeLog("StationConfigRead, FirmwareVersion: " & e.Device.FirmwareVersion.ToString)
        writeLog("StationConfigRead, CodeNumber: " & e.Device.CodeNumber.ToString)
        writeLog("StationConfigRead, SerialNumber: " & e.Device.SerialNumber.ToString)
    End Sub

    Private Sub reader_InputDeviceStateChanged(sender As Object, e As ReaderDeviceStateChangedEventArgs)
        writeLog("InputDeviceStateChanged: " & e.PreviousState.ToString & " => " & e.CurrentState.ToString)
    End Sub

    Private Sub reader_InputDeviceChanged(sender As Object, e As ReaderDeviceChangedEventArgs)
        writeLog("InputDeviceChanged: " & e.PreviousDevice.ToString & " => " & e.CurrentDevice.ToString)
    End Sub

    Private Sub reader_CardRead(sender As Object, e As SportidentDataEventArgs)
        MessageBox.Show("got here")

        Dim card As SportidentCard = e.Cards(0)

        writeLog("Card read: " & card.Siid.ToString)
    End Sub

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        writeLog("Refresh pressed")
        clearSIDevices()
        getSIDevices()
    End Sub

    Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click
        writeLog("Connect pressed")
        connect()
    End Sub

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        writeLog("Disconnect pressed")
        disconnect()
    End Sub

    Private Sub writeLog(Text As String)

        If listBox_log.InvokeRequired Then
            listBox_log.Invoke(New Action(Sub() writeLog(Text)))
        Else
            listBox_log.Items.Add(Text)
            listBox_log.SelectedIndex = listBox_log.Items.Count - 1
            listBox_log.SelectedIndex = -1
        End If
    End Sub

    Private Sub clearSIDevices()
        writeLog("Clearing list of devices")
        cbbSportIdentDevices.Items.Clear()
    End Sub

    Private Sub getSIDevices()
        writeLog("Scanning for available devices")

        ReaderDeviceInfo.GetAvailableDeviceList()

        For Each device As ReaderDeviceInfo In ReaderDeviceInfo.AvailableDevices
            cbbSportIdentDevices.Items.Add(device)
            writeLog("Adding device:  " & device.DeviceName.ToString)
        Next
        If cbbSportIdentDevices.Items.Count > 0 Then
            writeLog("Found " & cbbSportIdentDevices.Items.Count.ToString & " devices")
            cbbSportIdentDevices.SelectedIndex = 0
        Else
            writeLog("No devices found")
        End If


    End Sub

    Private Sub connect()

        Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
        reader.InputDevice = readerDeviceInfo
        reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None)
        reader.OpenInputDevice()
        reader.OpenOutputDevice()

    End Sub

    Private Sub disconnect()
        'reader = New Reader()
        cbbSportIdentDevices.Enabled = True
        reader.CloseInputDevice()
    End Sub

End Class



The exception seems to come when I have called the connect sub. I am assuming it doesn't like the 4 lines where I reference "reader".

Private Sub connect()

    Dim readerDeviceInfo As ReaderDeviceInfo = CType((cbbSportIdentDevices.SelectedItem), ReaderDeviceInfo)
    reader.InputDevice = readerDeviceInfo
    reader.OutputDevice = New ReaderDeviceInfo(ReaderDeviceType.None)
    reader.OpenInputDevice()
    reader.OpenOutputDevice()

End Sub

Shane


modified 21-Jul-17 7:43am.

AnswerRe: Converting C# to VB.net but code gives null reference Pin
Richard MacCutchan21-Jul-17 1:15
mveRichard MacCutchan21-Jul-17 1:15 
AnswerRe: Converting C# to VB.net but code gives null reference Pin
Ralf Meier21-Jul-17 2:33
mveRalf Meier21-Jul-17 2:33 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Shane James21-Jul-17 3:03
Shane James21-Jul-17 3:03 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Ralf Meier21-Jul-17 3:16
mveRalf Meier21-Jul-17 3:16 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Shane James21-Jul-17 4:30
Shane James21-Jul-17 4:30 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Ralf Meier21-Jul-17 8:44
mveRalf Meier21-Jul-17 8:44 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Shane James21-Jul-17 21:00
Shane James21-Jul-17 21:00 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Shane James21-Jul-17 21:02
Shane James21-Jul-17 21:02 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Ralf Meier22-Jul-17 10:25
mveRalf Meier22-Jul-17 10:25 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Pawel Wzietek1-Aug-17 2:33
Pawel Wzietek1-Aug-17 2:33 
GeneralRe: Converting C# to VB.net but code gives null reference Pin
Eddy Vluggen3-Aug-17 0:06
professionalEddy Vluggen3-Aug-17 0:06 
QuestionAccess VBA - Return Object From Function Pin
Kevin Marois20-Jul-17 20:13
professionalKevin Marois20-Jul-17 20:13 
SuggestionRe: Access VBA - Return Object From Function Pin
Richard MacCutchan20-Jul-17 20:37
mveRichard MacCutchan20-Jul-17 20:37 
GeneralRe: Access VBA - Return Object From Function Pin
Kevin Marois21-Jul-17 5:10
professionalKevin Marois21-Jul-17 5:10 
GeneralRe: Access VBA - Return Object From Function Pin
Richard MacCutchan21-Jul-17 6:11
mveRichard MacCutchan21-Jul-17 6:11 
GeneralRe: Access VBA - Return Object From Function Pin
Kevin Marois21-Jul-17 6:14
professionalKevin Marois21-Jul-17 6:14 
GeneralRe: Access VBA - Return Object From Function Pin
Richard MacCutchan21-Jul-17 6:34
mveRichard MacCutchan21-Jul-17 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.