将数组保存到文件

时间:2015-03-18 01:46:34

标签: vb.net

我一直在尝试将数组保存到文件中,但我只能创建文本文件,但实际文件中不会显示任何文本。循环不按我想要的方式工作。我需要将Cust数组存储到文本文件中。

Dim car(4) As Decimal
Dim Cust(4) As String

'Declare module-level constants.
Const PER_DAY_Integer As Integer = 15
Const PER_MILE_Decimal As Decimal = 0.12D



Private Sub CalcButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalcButton.Click
    'Calculate the rental charges


    Cust(0) = NameTextBox.Text
    Cust(1) = AddressTextBox.Text
    Cust(2) = CityTextBox.Text
    Cust(3) = ZipTextBox.Text
    Cust(4) = StateTextBox.Text

    Try

        car(0) = Decimal.Parse(BeginTextBox.Text)

        Try
            car(1) = Decimal.Parse(EndTextBox.Text)
            Try

                car(2) = Integer.Parse(DaysTextBox.Text)


                'Calculate the number of miles driven and the charges
                car(3) = car(1) - car(0)
                car(4) = (car(3) * PER_MILE_Decimal + car(2) * PER_DAY_Integer)

                'Display the results
                TotalTextBox.Text = car(4).ToString("C")
                MilesTextBox.Text = car(3).ToString("N1")




            Catch ex As Exception

                MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                DaysTextBox.Focus()

            End Try
        Catch ex As Exception

            MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            EndTextBox.Focus()

        End Try

    Catch ex As Exception

        MessageBox.Show("Input Error", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        BeginTextBox.Focus()

    End Try


End Sub

Private Sub ClearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearButton.Click
    'Clear the form

    AddressTextBox.Clear()
    CityTextBox.Clear()
    StateTextBox.Clear()
    ZipTextBox.Clear()
    BeginTextBox.Clear()
    EndTextBox.Clear()
    DaysTextBox.Clear()
    TotalTextBox.Clear()
    MilesTextBox.Clear()
    With NameTextBox
        .Clear()
        .Focus()
    End With
End Sub

Private Sub ExitButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitButton.Click
    'End the program

    Me.Close()
End Sub

Private Sub PrintButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintButton.Click
    'Print the form

    PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    PrintForm1.Print()
End Sub




Private Sub readBtn_Click(sender As Object, e As EventArgs) Handles readBtn.Click

End Sub

Private Sub WriteBtn_Click(sender As Object, e As EventArgs) Handles WriteBtn.Click

    Dim outFile As IO.StreamWriter
    Dim int As Integer = 0
    Dim count As Integer = 0

    outFile = IO.File.CreateText("Cust.txt")


    Do While int < count
        outFile.WriteLine(Cust(1)(int))
        int += 1
    Loop

    outFile.Close()

End Sub

结束班

1 个答案:

答案 0 :(得分:2)

你被问到调试失败的地方,你显然甚至没有打扰调试,或者你已经看到了这个问题。从你的代码看这个:

Dim int As Integer = 0
Dim count As Integer = 0

outFile = IO.File.CreateText("Cust.txt")


Do While int < count
    outFile.WriteLine(Cust(1)(int))
    int += 1
Loop

int是否会低于count

您可以用一行替换最后一个事件处理程序的全部内容:

IO.File.WriteAllLines("Cust.txt", Cust)

当然,您不应该单独使用文件名,而应该使用文件路径,但这是另一个问题。