簡體   English   中英

.dat文件中的Visual Basic 2d數組

[英]Visual Basic 2d array from .dat file

所以我試圖從兩個文本文件中提取一些數據。 第一個加載並完美填充列表框。 第二個文本文件是我遇到麻煩的地方。 我似乎無法正確加載它。 我正在嘗試將其放入2D數組MileageAr。 該消息框用於解決它是否正確加載到陣列中的問題。 我究竟做錯了什么?

來自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

末級

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

將此更改為Mileage.Peek

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方法可能很好,我在處理文本文件時通常只使用上面的代碼,可能值得一試...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM