Log in

View Full Version : Reading from files with 2D arrays - VB.NET



Richard
03-03-2011, 10:56 PM
I know there is a specific VB forum here, but it's possibly collected the most dust out of any subforum.

My issues is that I get a runtime when running the below proc:


Private Sub cmdAddRecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRecipe.Click
Dim objRead As System.IO.StreamReader
Dim i, ii, fileLength As Integer
Dim recipes As String
Dim tempString(1)() As String
Dim items()() As String
objRead = IO.File.OpenText("E:\Computing stuffs\JuiceBar\recipes.txt")
recipes = objRead.ReadToEnd()
While Not objRead.EndOfStream
objRead.ReadLine()
fileLength = fileLength + 1
End While
objRead.Dispose()
objRead.Close()
For i = 0 To fileLength
tempString(0) = Split(recipes, "|")
For ii = 0 To 24
items(i)(ii) = " " '<- this line
tempString(1) = Split(tempString(0)(i), ",")
items(i)(ii) = tempString(1)(i)
Next
Next
End Sub

The runtime states that I can't use the variable "items", as it hasn't been assigned a value. As you can see, I have attempted to assign it a value before it gains the true one, to test the error, but still no.

Ideas?

i luffs yeww
03-04-2011, 03:13 AM
Isn't items reserved by VB? I can't remember if that would change anything, but it's my first thought.

And what if you set it outside of the loop?

Richard
03-04-2011, 10:40 AM
Isn't items reserved by VB? I can't remember if that would change anything, but it's my first thought.

And what if you set it outside of the loop?

It might be, I'm not sure.

If I had to set it outside the loop, then it would take forever to do so, with a potentially massive number of pieces of data to go into the array.

i luffs yeww
03-04-2011, 11:19 AM
wat

I mean if you set it to an empty string out of the loop.

Richard
03-04-2011, 10:11 PM
wat

I mean if you set it to an empty string out of the loop.

Tried that; unsuccessful. Instead I've just made 2 seperate arrays, with the second splitting from the first, x number of times. It still has errors in reading though. Ah well

EDIT: Entire thing works now, the code is as follows, for those interested in knowing the fix:


Private Sub cmdAddRecipe_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddRecipe.Click
Dim newName As String
Dim objFile As New System.IO.StreamWriter("E:\Computing stuffs\JuiceBar\recipes.txt", True)
newName = InputBox("Choose a name for the new juice drink that you have created!", "Name your drink")
Try
objFile.Write(newName & ",")
For i = 0 To 11
objFile.Write(fruits(i).isSelected & ",")
objFile.Write(fruits(i).fruitContent & ",")
Next
objFile.WriteLine("|")
objFile.Close()
MsgBox("Fruit juice drink has been added")
Catch ex As Exception
MsgBox("Error adding juice drink to the database")
End Try
End Sub