简体   繁体   English

.dat文件中的Visual Basic 2d数组

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

So I'm trying to pull some data from two text files. 所以我试图从两个文本文件中提取一些数据。 The first loads and populates the listboxes perfectly. 第一个加载并完美填充列表框。 The second text file is where I'm having trouble. 第二个文本文件是我遇到麻烦的地方。 I can't seem to get it to load correctly. 我似乎无法正确加载它。 I'm trying to put it into a 2D array, MileageAr. 我正在尝试将其放入2D数组MileageAr。 The messagebox is to troubleshoot if it is loading into the array correctly. 该消息框用于解决它是否正确加载到阵列中的问题。 What am I doing wrong? 我究竟做错了什么?

Sample Data from SDMileage.dat 来自SDMileage.dat的样本数据

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

etc.... 等等....

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

End Class 末级

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

CHANGE THIS TO Mileage.Peek 将此更改为Mileage.Peek

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

Try looping this way instead 尝试以这种方式循环

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

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

The Peek method might be fine, I just typically use the code above when working with text files, might be worth a shot... Peek方法可能很好,我在处理文本文件时通常只使用上面的代码,可能值得一试...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM