用文本文件填充两个一维数组
对于我的 Visual Basic 期末考试,我的程序需要将文本文件中的数据读取到两个不同的数组中,每个数组都是一维的。以下是我这样做的代码:
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
文本文件交替读取 DVD 的名称和价格作为单独的行,使阵列平行:
Pulp Fiction
9.99
Jumanji
13.99
And so on...
我收到一个值类型错误代码,指出在将数组的值设置为等于它们各自的变量时,我无法将 'String' 转换为 'String()' 或将 'Double' 转换为 'Double()'。有没有办法纠正这个问题?提前致谢!
回答
这些行是错误的:
arrayDVD = dvdName
arrayPrice = dvdPrice
arrayDVD并且arrayPrice是数组。您需要分配给每个数组中的特定元素:
arrayDVD(i) = dvdName
arrayPrice(i) = dvdPrice
不要忘记确保数组实际上有足够的元素。
提示:ReDim Preserve几乎是确保数组足够大的最低效率方法。每次使用都会分配一个全新的数组,一次复制一个元素,将新数组赋值给旧引用,然后释放旧数组。它并没有就地保护。尽管如此,如果这是一门 100 级的课程,那么此时您可能需要完成这些课程。
最后,在与金钱打交道Double时永远不要使用(Decimal改为使用)。
与问题分开,这里是我如何在没有奇怪数组限制的情况下解决这个问题:
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