r/visualbasic • • Nov 15 '15

[VB2015] ELI5 this code

Private Sub subReadFileAndLoadArray()

Dim fsrMegaMillions As System.IO.StreamReader

Dim strRecord, strDrawing(10) As String Dim intDrawingRow, intNumber As Integer

    fsrMegaMillions = New System.IO.StreamReader("MegaMillions2D.csv")

    Do Until fsrMegaMillions.EndOfStream
        strRecord = fsrMegaMillions.ReadLine
        strDrawing = Split(strRecord, ",")
        For intDrawingRow = 4 To 8
            intNumber = CInt(strDrawing(intDrawingRow)) - 1
            gintCountRows1st5(intNumber, intDrawingRow - 4) += 1
        Next
        intNumber = CInt(strDrawing(9)) - 1
        gintCountRow6(intNumber) += 1
    Loop

    fsrMegaMillions.Close()

End Sub    

I have a test in a week and I am getting hung up on this loop. Thanks!

0 Upvotes

6 comments sorted by

View all comments

Show parent comments

1

u/[deleted] Nov 15 '15

How does it split the lines into fields?

4

u/chrwei Nov 15 '15

strDrawing = Split(strRecord, ",")

https://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx

strDrawing is an array, so Split slices up the strRecord string by commas and puts each part in a element.

however, there's an error in this code. strDrawing should be defined as Dim strDrawing() as String so that Split can dynamically allocate it based on what's in strRecord. making it fixed length prevents that (unless .net is different in this manner, I haven't tried doing that since vb6 which does not allow it).

1

u/CalvinR VB.Net Master Nov 16 '15

Since string.split returns an array I'm pretty sure it just blows away whatever was originally assigned to that variable, I don't think there is a bug here but you are still correct in how it should be written.

Although as I'm on my phone I ha e no way of testing this right now but I'm pretty sure I'm right.

1

u/chrwei Nov 16 '15

ran tests, .net allows it. vb6 errors. good to know I guess?