Dim MapString(41, 7) As String
Private Sub MapForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

'mapfile to array
Dim StrSplit(), LineRead As String
Dim x, y As Integer
'open the file
Dim sreReader As StreamReader = New StreamReader("MapFile.txt")
'x and y must be to 0
x = 0
y = 0
Do
LineRead = sreReader.ReadLine
'Split the line into strSplit string
StrSplit = LineRead.Split(","c)
Do While y < 8
MapString(x, y) = StrSplit(y)
y += 1
Loop

x += 1
y = 0
'Loop until no line
Loop Until sreReader.Peek = -1
sreReader.Close()

I now keep getting "Index was outside the bounds of the array." when it reaches "MapString(x, y) = StrSplit(y)".
the text file looks like this:
t1,None,None,None,0,None,None,None
t2,None,None,None,0,None,None,None
t3,None,None,None,0,None,None,None
.......
t42,None,None,None,0,None,None,None
I just don't understand why this happens, can anyone help?

What I am trying to do:
I'm making a board games of sorts. There are 40 some sections that need to have roughly 7 (at this point, might have more in the end, might have less) aspects about them that may change during the game, but stay around next time you play the game. What I'm trying to do here is load the text file with all the info about the board into a giant array. Then during game play I want to be able look up just one piece of info about it at a time (depending on the circumstance in the game). example: if i want to check to see if a the 25th spot on the board has a city i may use this code:

select case MapString(24,2)
        case "None"
        case "small"
            Population += 1
        case "large"
            Population += 3

Or if a city is founded in the 17th spot on the :

MapString(16,2) = small

Then at the end of the game the array gets saved back into the text file for the next game.
Right now though, I'm just trying to get the text file into an array.

You know what, I figured it out. turns out i messed up making the text files and put the same line twice. that made the count 43, not the intended 42. I was focusing on the creation of the array from the text that I didnt notice my problem was in the declaration. SIMPLE. HUMAN. ERROR. Lessoned learned on that one.

I have found that for smaller files, it is easier to let the system read all the lines instead of bothering with streams and such.

Dim row As Integer = 0

For Each line As String In System.IO.File.ReadAllLines(MAPFILE)

    Dim flds() As String = line.Split(","c)

    For col As Integer = 0 To UBound(flds)
        MapString(row, col) = flds(col)
    Next

    row += 1

Next

That way you don't have to worry about opening, closing and testing for end of stream.

Reverend Jim, I finally got some free time to play with my code and even though my code ran fine, when i began changing the variables in the text manually to make sure it worked, nothing changed. It clearly didn't work even though it had no errors. I Slapped your code in place of mine and BAMN! working smoothly. I've only takien one course on this at my community college and only been using Visual Studios for a few months so I'm still green around the gills, so THANK YOU SO VERY MUCH for saving me a night of hassle and being able to get back to the parts I know and enjoy.

This article has been dead for over six months. Start a new discussion instead.