来自.dat文件的Visual Basic 2d数组

时间:2011-10-02 03:40:57

标签: .net vb.net visual-studio-2010

所以我试图从两个文本文件中提取一些数据。第一个加载并完美填充列表框。第二个文本文件是我遇到麻烦的地方。我似乎无法正确加载它。我正试图把它放到一个二维数组中,前程万里。如果正确加载到阵列中,则消息框将进行故障排除。我究竟做错了什么?

SDMileage.dat的示例数据

1,54,465,58,488,484
5,54,654,87,841,844

等......

Public Class ArrayFun
Dim MileageAr(10, 10) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim State As New System.IO.StreamReader("SDCities.dat")
    Dim strState As String
    Dim i, j As Integer
    Do While State.Peek <> -1
        strState = State.ReadLine
        lstFrom.Items.Add(strState)
        lstTo.Items.Add(strState)
    Loop


    Dim Mileage As New System.IO.StreamReader("SDMileage.dat")
    Dim strLine As String
    Dim strSplit() As String

    Do While State.Peek <> -1
        strLine = Mileage.ReadLine
        strSplit = strLine.Split(",")
        j = 0
        For Each miles In strSplit
            MileageAr(i, j) = miles
            j += 1
        Next
        i += 1
    Loop
    State.Close()
    Mileage.Close()

End Sub


Private Sub lstTo_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstTo.SelectedIndexChanged
    MsgBox(MileageAr(1, 1))
End Sub

结束班

1 个答案:

答案 0 :(得分:1)

Dim Mileage As New System.IO.StreamReader("SDMileage.dat")
Dim strLine As String
Dim strSplit() As String

将此更改为“前程万里”。

Do While State.Peek <> -1
    strLine = Mileage.ReadLine
    strSplit = strLine.Split(",")

尝试以这种方式循环

Dim Mileage As System.IO.TextReader = New StreamReader("SDMileaage.dat")

do while ((strline = Mileage.ReadLine) <> nothing)

Peek方法可能没问题,我通常在处理文本文件时使用上面的代码,可能值得一试......