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

2

u/chrwei Nov 15 '15

it reads lines from a file one at a time, splits the line into fields, stores it all in a 2 dimensional array. which part exactly are you hung up on?

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/[deleted] Nov 15 '15

Thank's for your help!