I have some data in a text file in the format:
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,I,O,O,S,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,O,O,O,O,O,O,O,O,O,O,X,X,X
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
X,X,X,X,X,X,X,X,X,X,X,X,X,X,X,X
and I want to load it to an array (e.g. myArray) which is declared as:
Public myArray(15,15) As String
I can load the first line:

How can I load data into a 2-dimensional array?
skier17
Create the console application and you will see it works.
How you are using that array in your code is the problem, not loading the array. Are you passing into a method and doing this in the method - in this case are you passing byref and not byval.
Is you array declaraed as a class level variable
The array is definately loaded correctly.
Ludy
Check that your processing rows and columns in the right order.
As you have one loop nested inside the other - if they were switched you would get a 90 rotation.
HIRU
So your first problem with you code is you have a class level variable
Dim mapArray(15, 15) As String
and a similar declaration in the form load.
Therefore when your loading it will use the one in the load which is only scoped private to the load procedure.
GS3422
i now have this code which doesn't work still:
Dim mapArray(15, 15) As String Dim i As Integer Dim j As Integer Dim loadArray(15) As String Dim filename As String = "C:\ArrPeeGee\Map.txt" Dim delimiter As String = "," Dim j As Integer = 0 If My.Computer.FileSystem.FileExists(filename) = True Then Using parser As New TextFieldParser(filename)parser.SetDelimiters(delimiter)
While Not parser.EndOfDataloadArray = parser.ReadFields()
For i = 0 To 15mapArray(j, i) = loadArray(i)
Next ij = j + 1
End While End Using End IfJerome Le Bateau
Todd Wilder
OK Based upon the file contents you provided earlier and, this code works - its a console application and reads in the file into the array and then output the array.
Your description of it doesnt work is very vague - what doesnt work. What error is it producing, results are not occuring.
Module Module1
Sub Main()
Dim mapArray(15, 15) As String
Dim i As Integer
Dim j As Integer = 0
Dim loadArray(15) As String
Dim filename As String = "C:\foo.txt"
Dim delimiter As String = ","
If My.Computer.FileSystem.FileExists(filename) = True Then
Using parser As New Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
loadArray = parser.ReadFields()
For i = 0 To 15
mapArray(j, i) = loadArray(i)
Next i
j = j + 1
End While
End Using
'//Display the mapparray results
For i = 0 To 15
For j = 0 To 15
Console.Write(mapArray(i, j))
Next
Console.WriteLine("")
Next
End If
End Sub
End Module
firewalker
The article you want to look at the samples is
http://msdn2.microsoft.com/en-us/library/cakac7e6.aspx
which shows using the text field parser class
Looking at the readfiles method of this class it reads all the items in the current line
http://msdn2.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readfields.aspx
which is great but you still need to wrap this with a loop for multiple lines
While Not parser.EndOfData
....
Loop
Each line you read would cause an increment of a variable used to set the first dimension of the array which youve currently got coded to 0
Kaa
Ok, i'm running the code in a windows form (that imports Microsoft.VisualBasic.FileIO) as part of an application which will later use mapArray to find pieces (tiles) of a map and display them. When loaded, the form shows a blank map (not even 'empty' tiles) although no error is given. When I add loadArray to the Watch window it gives 'Unable to evaluate the expression'.
The given bit of code runs in the event handler for the form loading.
I wouldn't mind inputing to the program by hand (i.e. typing in mapArray(0) = X,X,X,X,X,X,X,X...etc.) but don't want to go mapArray(0,0) = "X", mapArray(0,1) = "X" etc.
PlasticLizard
This is the entire code for the form since I can't work out what it is.
PictureBox43, for example, refers to the PictureBox in the 4th column in the 3rd row.
Imports
Microsoft.VisualBasic.FileIOPublic
Class APGMap Dim mapArray(15, 15) As String Dim i As Integer Dim j As Integer Dim loadArray(15) As String Dim messageString As String Dim image As Object Dim XL As Integer Dim YL As Integer Dim tRandom As Integer Dim blanktile As Object = ArrPeeGee.My.Resources.Resources.blanktile Public Sub repaint()XL = APGMainMenu.playerLocation(0)
YL = APGMainMenu.playerLocation(1)
PictureBox44.Image = ArrPeeGee.My.Resources.Resources.playertile
getimg(-3, -3)
PictureBox11.Image = image
getimg(-3, -2)
PictureBox12.Image = image
getimg(-3, -1)
PictureBox13.Image = image
getimg(-3, 0)
PictureBox14.Image = image
getimg(-3, 1)
PictureBox15.Image = image
getimg(-3, 2)
PictureBox16.Image = image
getimg(-3, 3)
PictureBox17.Image = image
getimg(-2, -3)
PictureBox21.Image = image
getimg(-2, -2)
PictureBox22.Image = image
getimg(-2, -1)
PictureBox23.Image = image
getimg(-2, 0)
PictureBox24.Image = image
getimg(-2, 1)
PictureBox25.Image = image
getimg(-2, 2)
PictureBox26.Image = image
getimg(-2, 3)
PictureBox27.Image = image
getimg(-1, -3)
PictureBox31.Image = image
getimg(-1, -2)
PictureBox32.Image = image
getimg(-1, -1)
PictureBox33.Image = image
getimg(-1, 0)
PictureBox34.Image = image
getimg(-1, 1)
PictureBox35.Image = image
getimg(-1, 2)
PictureBox36.Image = image
getimg(-1, 3)
PictureBox37.Image = image
getimg(0, -3)
PictureBox41.Image = image
getimg(0, -2)
PictureBox42.Image = image
getimg(0, -1)
PictureBox43.Image = image
getimg(0, 1)
PictureBox45.Image = image
getimg(0, 2)
PictureBox46.Image = image
getimg(0, 3)
PictureBox47.Image = image
getimg(1, -3)
PictureBox51.Image = image
getimg(1, -2)
PictureBox52.Image = image
getimg(1, -1)
PictureBox53.Image = image
getimg(1, 0)
PictureBox54.Image = image
getimg(1, 1)
PictureBox55.Image = image
getimg(1, 2)
PictureBox56.Image = image
getimg(1, 3)
PictureBox57.Image = image
getimg(2, -3)
PictureBox61.Image = image
getimg(2, -2)
PictureBox62.Image = image
getimg(2, -1)
PictureBox63.Image = image
getimg(2, 0)
PictureBox64.Image = image
getimg(2, 1)
PictureBox65.Image = image
getimg(2, 2)
PictureBox66.Image = image
getimg(2, 3)
PictureBox67.Image = image
getimg(3, -3)
PictureBox71.Image = image
getimg(3, -2)
PictureBox72.Image = image
getimg(3, -1)
PictureBox73.Image = image
getimg(3, 0)
PictureBox74.Image = image
getimg(3, 1)
PictureBox75.Image = image
getimg(3, 2)
PictureBox76.Image = image
getimg(3, 3)
PictureBox77.Image = image
getimg(0, 0)
If mapArray(XL, YL) = "O" ThentRandom = Int(Rnd() * 100)
If tRandom >= 0 Then APGFight.Fight("Random Attack", 1, 1, 1, 1, 1) End If End Sub Public Sub getimg(ByVal X As Integer, ByVal Y As Integer) Select Case mapArray(XL + X, YL + Y) Case "X"image = ArrPeeGee.My.Resources.Resources.mountaintile
Case "O"image = ArrPeeGee.My.Resources.Resources.blanktile
Case "S"image = ArrPeeGee.My.Resources.Resources.shoptile
Case "I"image = ArrPeeGee.My.Resources.Resources.inntile
End Select End Sub Private Sub APGMap_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim mapArray(15, 15) As String Dim i As Integer Dim j As Integer = 0 Dim loadArray(15) As String Dim filename As String = "C:\ArrPeeGee\Map.txt" Dim delimiter As String = "," If My.Computer.FileSystem.FileExists(filename) = True Then Using parser As New TextFieldParser(filename)parser.SetDelimiters(delimiter)
While Not parser.EndOfDataloadArray = parser.ReadFields()
For i = 0 To 15mapArray(j, i) = loadArray(i)
Next ij = j + 1
End While End Using End IfMsgBox(mapArray(7, 7))
repaint()
End Sub Private Sub NorthButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NorthButton.Click Select Case mapArray(XL, YL + 1) Case "X"MsgBox(
"You cannot move into mountains.") Case "O"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) + 1
Case "S"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) + 1
APGShop.Show()
Case "I"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) + 1
APGHeal.Show()
End Selectrepaint()
End Sub Private Sub EastButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EastButton.Click Select Case mapArray(XL - 1, YL) Case "X"MsgBox(
"You cannot move into mountains.") Case "O"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) - 1
Case "S"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) - 1
APGShop.Show()
Case "I"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) - 1
APGHeal.Show()
End Selectrepaint()
End Sub Private Sub WestButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles WestButton.Click Select Case mapArray(APGMainMenu.playerLocation(1) + 1, APGMainMenu.playerLocation(0)) Case "X"MsgBox(
"You cannot move into mountains.") Case "O"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) + 1
Case "S"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) + 1
APGShop.Show()
Case "I"APGMainMenu.playerLocation(1) = APGMainMenu.playerLocation(1) + 1
APGHeal.Show()
End Selectrepaint()
End Sub Private Sub SouthButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SouthButton.Click Select Case mapArray(APGMainMenu.playerLocation(1), APGMainMenu.playerLocation(0) - 1) Case "X"MsgBox(
"You cannot move into mountains.") Case "O"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) - 1
Case "S"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) - 1
APGShop.Show()
Case "I"APGMainMenu.playerLocation(0) = APGMainMenu.playerLocation(0) - 1
APGHeal.Show()
End Selectrepaint()
End SubEnd
Class