Results 1 to 5 of 5

Thread: Reading from files with 2D arrays - VB.NET

  1. #1
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default Reading from files with 2D arrays - VB.NET

    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:

    Code:
    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?
    Last edited by Richard; 03-03-2011 at 10:59 PM.

  2. #2
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    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?

  3. #3
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    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.

  4. #4
    Join Date
    Jan 2010
    Posts
    5,227
    Mentioned
    6 Post(s)
    Quoted
    60 Post(s)

    Default

    wat

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

  5. #5
    Join Date
    Dec 2007
    Location
    192.168.1.73
    Posts
    2,439
    Mentioned
    6 Post(s)
    Quoted
    119 Post(s)

    Default

    Quote Originally Posted by i luffs yeww View Post
    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:

    Code:
    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
    Last edited by Richard; 03-04-2011 at 10:15 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •