Populating two one-dimensional arrays with text file

Viewed 165

For my Visual Basic final, my program is required to read data from a text file into two different arrays, each being one-dimensional. The following is my code for doing so:

Option Explicit On
Option Infer Off
Option Strict On

Public Class frmMain

    'Constant for filename and a dirty flag variable
    Const INVENTORY_FILENAME As String = "inventory.txt"
    Dim noFile As Boolean = False

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Populates DVD listbox with text file data upon load

        'Variable for reading the file
        Dim myFile As IO.StreamReader

        'Declaring arrays for DVD names and prices
        Dim arrayDVD() As String
        Dim arrayPrice() As Double

        'Variables for populating arrays with respective data
        Dim dvdName As String
        Dim dvdPrice As Double
        Dim i As Integer = 0

        'Checking that file exists then reading data to each array
        If IO.File.Exists(INVENTORY_FILENAME) Then
            myFile = IO.File.OpenText(INVENTORY_FILENAME)

            'Read data to arrays
            Do Until myFile.Peek = -1
                dvdName = myFile.ReadLine()
                dvdPrice = Double.Parse(myFile.ReadLine())

                arrayDVD = dvdName
                arrayPrice = dvdPrice

                'Using arrays to populate multicolumn listbox
                lstDVD.Items.Add(arrayDVD(i) & arrayPrice(i))

                i += 1

            Loop

            'Closing the file
            myFile.Close()

        End If

    End Sub

End Class

The text file alternates names and prices of DVDs to be read as individual lines, making the arrays parallel:

Pulp Fiction
9.99
Jumanji
13.99
And so on...

I'm receiving a value type error code stating that I cannot convert 'String' to 'String()' or convert 'Double' to 'Double()' when setting the arrays' values equal to their respective variables. Is there a way to correct this? Thanks in advance!

1 Answers

These lines are wrong:

arrayDVD = dvdName
arrayPrice = dvdPrice

arrayDVD and arrayPrice are arrays. You need to assign to a specific element in each of those arrays:

arrayDVD(i) = dvdName
arrayPrice(i) = dvdPrice

Don't forget to make sure the arrays actually have enough elements for this.

Hint: ReDim Preserve is pretty much the least efficient way possible to make sure an array is big enough. Each use will allocate a brand new array, copy the elements one at a time, assign the new array to the old reference, and then release the old array. It does not preserve in-place. Nevertheless, if this is a 100-level course it might be what you are expected to do at this point.

Finally, you should never use Double when working with money (use Decimal instead).


Separate from the question, here is how I might approach this without the weird array limitation:

Private Iterator Function ReadInventoryFile(filePath As String) As IEnumerable(Of (String, Decimal))
    Using rdr As New StreamReader(filePath)
          Dim DVDName As String = Nothing
          While (DVDName = rdr.ReadLine()) IsNot Nothing
                Yield (DVDName, Decimal.Parse(rdr.ReadLine()))
          End While
    End Using
End Function

Const INVENTORY_FILENAME As String = "inventory.txt"
Private data As List(Of (String, Decimal))

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Try 'Replaces the File.Exists() check
        data = ReadInventoryFile(INVENTORY_FILENAME).ToList()

        For Each item As (String, Decimal) In data
            lstDVD.Items.Add($"{item.Item1}{vbTab}{item.Item2:C}")
        Next
    Catch
        ' Actually do something here. Empty catch blocks are rarely correct.
        ' Note I catch at this level, rather than in the ReadFile() method.
    End Try
End Sub
Related