I was following some code presented by another developer and for some reason the program is not reading the txt file that I created with the csv data.

What am I doing wrong?

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class ConverterUpdated
    Dim UnitConvVal As Double ' Global Variable for Unit Value.
    Public Sub DataLoad(ByVal Switch As String)
        Dim tfLines() As String = File.ReadAllLines("C:\data.txt") ' File to load.
        For Each line As String In tfLines ' Load and read all lines in file.
            Dim field As String() = line.Split(","c) ' Split using ,.

            Select Case Switch
                Case "LoadCategory"
                    If field(0) = "Category" Then
                        cboCategory.Items.Add(field(1))
                        cboCategory.SelectedIndex = 0 ' Auto Select first Category.
                    End If

                Case "LoadUnits"
                    If field(0) = CStr(cboCategory.SelectedItem) Then
                        cboConvertTo.Items.Add(field(1))
                        cboConvertTo.SelectedIndex = 0 ' Auto Select first Unit.
                    End If

                Case "SelectUnit"
                    If field(1) = CStr(cboConvertTo.SelectedItem) Then
                        UnitConvVal = CDbl(field(2)) ' Load Unit's Conversion value, store in UnitConvVal as Double.
                    End If
            End Select
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        DataLoad("LoadCategory") ' Switch to DataLoad to load Categories on form load.
    End Sub

    Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        cboConvertTo.Items.Clear() ' Clear cboConvertTo Combobox when re-loading units after selection change.
        DataLoad("LoadUnits")
    End Sub

    Private Sub cboConvertTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        DataLoad("SelectUnit")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If txtInputVal.Text = Nothing Then
            MessageBox.Show("Please Enter A Value to Convert") ' Display message if no value is entered.
        Else
            txtResult.Text = CStr(CDbl(txtInputVal.Text) * UnitConvVal) ' If Value has been entered, compute conversion for unit.
        End If
    End Sub
End Class

This is the information inside C:\data.txt

Category,Weight
Category,Distance
Weight,LB to KG,0.45359237
Weight,KG to LB,2.20462262
Distance,Mile to KM,1.609344
Distance,KM to Mile,0.621371192

Edited 5 Years Ago by tstory28: Added data.txt information

OK. I went back through and found a few errors in my original VB code. Here is what I have updated and what the new error is.

Option Explicit On
Option Strict On
Imports System
Imports System.IO
Public Class ConverterUpdated
    Dim UnitConvVal As Double ' Global Variable for Unit Value.

    Public Sub DataLoad(ByVal Switch As String)
        Dim tfLines() As String = File.ReadAllLines("C:\data.txt") ' File to load.

        For Each line As String In tfLines ' Load and read all lines in file.
            Dim field As String() = line.Split(","c) ' Split using ,.
            Select Case Switch
                Case "LoadCategory"
                    If field(0) = "Category" Then
                        cboCategory.Items.Add(field(1))
                        cboCategory.SelectedIndex = 0 ' Auto Select first Category.
                    End If

                Case "LoadUnits"
                    If field(0) = CStr(cboCategory.SelectedItem) Then
                        cboConvertTo.Items.Add(field(1))
                        cboConvertTo.SelectedIndex = 0 ' Auto Select first Unit.
                    End If

                Case "SelectUnit"
                    If field(1) = CStr(cboConvertTo.SelectedItem) Then
                        UnitConvVal = CDbl(field(2)) ' Load Unit's Conversion value, store in UnitConvVal as Double.
                    End If
            End Select
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        DataLoad("LoadCategory") ' Switch to DataLoad to load Categories on form load.
    End Sub

    Private Sub cboCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCategory.SelectedIndexChanged
        cboConvertTo.Items.Clear() ' Clear cboConvertTo Combobox when re-loading units after selection change.
        DataLoad("LoadUnits")
    End Sub

    Private Sub cboConvertTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboConvertTo.SelectedIndexChanged
        DataLoad("SelectUnit")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        If txtInputVal.Text = Nothing Then
            MessageBox.Show("Please Enter A Value to Convert") ' Display message if no value is entered.
        Else
            txtResult.Text = CStr(CDbl(txtInputVal.Text) * UnitConvVal) ' If Value has been entered, compute conversion for unit.
        End If
    End Sub
End Class

Now I am getting an error of IndexOutOfRangeException was unhandled. What happened for that to come up and how do I fix it?

Hi,
The only place you're using an integer that cause the out of range exception is when you refer to field(1) or field(2). So I would check that your field, when split on the comma character actually has 3 resulting splits e.g. 2 commas are in the initial line.
Otherwise, if your line was "hello, goodbye" for example, there would be no field(2), just field(0) and field(1).
Hope that helps,

This article has been dead for over six months. Start a new discussion instead.