开发者

Visual Basic 2d array from .dat file

开发者 https://www.devze.com 2023-04-10 15:49 出处:网络
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 correctl

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. The messagebox is to troubleshoot if it is loading into the array correctly. What am I doing wrong?

Sample Da开发者_运维技巧ta from 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

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...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号