在为变量赋值空值之前使用变量。使用StreamReader

时间:2014-04-17 00:31:02

标签: vb.net

我决定尝试一下本书中的高级练习,需要我编辑一个程序。在获取艺术家姓名和价格之前, btn.Add_Click 过程应确定CD名称是否已包含在列表框中。如果列表框包含CD名称,则该过程应显示相应的消息,然后不将CD添加到列表中。问题是 Do Until inFile .Peek = -1 For Each Name As String In strNameCollection 显然是分配了一个空值。有关为什么会这样或可能如何使其工作的任何想法? btn.Add_Click位于代码底部

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub frmMain_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    ' save the list box information

    ' declare a StreamWriter variable
    Dim outFile As IO.StreamWriter
    ' open the file for output
    outFile = IO.File.CreateText("CDs.txt")
    ' write each line in the list box
    For intIndex As Integer = 0 To lstCds.Items.Count - 1
        outFile.WriteLine(lstCds.Items(intIndex))
    Next intIndex

    ' close the file
    outFile.Close()
End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' fills the list box with data 
    ' stored in a sequential access file

    ' declare variables
    Dim inFile As IO.StreamReader
    Dim strInfo As String

    ' verify that the file exists
    If IO.File.Exists("CDs.txt") Then
        ' open the file for input
        inFile = IO.File.OpenText("CDs.txt")

        ' process loop instructions until end of file
        Do Until inFile.Peek = -1
            strInfo = inFile.ReadLine
            lstCds.Items.Add(strInfo)
        Loop
        inFile.Close()

        ' select the first line in the list box
        lstCds.SelectedIndex = 0
    Else
        MessageBox.Show("Can't find the CDs.txt file",
                        "CD Collection",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Information)
    End If
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    ' adds CD information to the list box

    ' declare variables
    Dim strName As String
    Dim strArtist As String
    Dim strPrice As String
    Dim strConcatenatedInfo As String
    Dim dblPrice As Double
    Dim strNameCollection() As String

    'read all names into array 

    Dim inFile As IO.StreamReader

    If IO.File.Exists("CDs.txt") Then
        ' open the file for input
        inFile = IO.File.OpenText("CDs.txt")
    End If
    'read all names into array 
    Dim index As Integer = 0
    Do Until inFile.Peek = -1
        ReDim strNameCollection(index)
        strNameCollection(index) = inFile.ReadLine
    Loop
    inFile.Close()

    ' get the CD information 
    strName = InputBox("CD name:", "CD Collection")
    For Each Name As String In strNameCollection
        If strName = Name Then
            MessageBox.Show("Sorry that name was alread on the list", "CD Project",
                            MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
        Else
            strArtist = InputBox("Artist:", "CD Collection")
            strPrice = InputBox("Price:", "CD Collection")

            Double.TryParse(strPrice, dblPrice)
            strPrice = dblPrice.ToString("N2")

            strConcatenatedInfo = strName.PadRight(40) &
        strArtist.PadRight(25) & strPrice.PadLeft(5)
            lstCds.Items.Add(strConcatenatedInfo)
        End If
    Next Name

End Sub

Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
    ' removes the selected line from the list box

    ' if a line is selected, remove the line
    If lstCds.SelectedIndex <> -1 Then
        lstCds.Items.RemoveAt(lstCds.SelectedIndex)
    End If
End Sub
End Class

1 个答案:

答案 0 :(得分:3)

为什么不利用Using块?将strNameCollection设为List(Of String)而不是数组,而不是正确增加其大小。

Private strNameCollection As New List(Of String)
Using sr As New StreamReader("CDs.txt")
  While Not sr.EndOfStream
    strNameCollection.Add(sr.ReadLine)
  End while
End Using ' file closed and stream disposed