LMH
LMH

Reputation: 7

I keep getting and error message 'Index was outside the bounds of the array'

I am trying to display information from a text file into a multiline textbox. I run the code but the system displays an error message 'Index was outside the bounds of the array'. There are no obvious error messages and I can't seem to manipulate the code to get rid of this problem. Take a look:

Public Class TeachCon

Dim layout As String
Dim Contacts(6) As Details

Structure Details
    Dim Name As String
    Dim Email As String
    Dim RoomNum As String
    Dim number1, number2 As Integer
End Structure

Sub LoadTeachContacts(ByRef Contacts() As Details)

    Dim TextFile As String = "\\Sjcdom01\mstudent\LHeywood\documents\A2\Computing\Comp 4 - Smail\Project\Text Files\Teacher Contact List.txt"
    Dim TextLine As String = ""
    Dim ArrayCounter As Integer = 0
    Dim objReader As New System.IO.StreamReader(TextFile)

    'loop through text file and load all contacts 
    Do While objReader.Peek() <> -1

        'read next line from file
        TextLine = TextLine & objReader.ReadLine() & vbNewLine

        'declare an array and use it to split line from file
        Dim TempArray() As String = Split(TextLine, ",")

        'transfer each array element into the appropriate part of the contacts stucture
        Contacts(ArrayCounter).Name = TempArray(0)
        *Contacts(ArrayCounter).Email = TempArray(1)*
        Contacts(ArrayCounter).RoomNum = TempArray(2)
        Contacts(ArrayCounter).number1 = TempArray(3)
        Contacts(ArrayCounter).number2 = TempArray(4)

        'empty string before reading next line from file
        TextLine = ""

        'increment array counter
        ArrayCounter = ArrayCounter + 1
    Loop
End Sub


Private Sub ButShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    Dim ArrayCounter As Integer = 0
    LoadTeachContacts(Contacts)

    Do Until ArrayCounter = 3
        layout = Contacts(ArrayCounter).Name & "," & Contacts(ArrayCounter).Email & "," & Contacts(ArrayCounter).RoomNum & "," & Contacts(ArrayCounter).number1 & "," & Contacts(ArrayCounter).number2
        If ArrayCounter = 0 Then
            TextBox7.Text = layout
        End If

        ArrayCounter += 1
    Loop
End Sub

End Class

The text enclosed by the * is where the system says it is outside the bounds of the array.

Upvotes: 0

Views: 577

Answers (4)

PT_01
PT_01

Reputation: 156

I think that you should check if the line is empty or not, because the item 0 will be available without error as a Null String, but the item 1 will throw 'Index was outside the bounds of the array' In LoadTeachContacts Sub

'read next line from file
            If objReader.ReadLine().Trim = "" Then Continue Do
            TextLine = TextLine & objReader.ReadLine() & vbNewLine

Upvotes: 0

Pankti Shah
Pankti Shah

Reputation: 133

It would be helpful if you could give the content of the file also:

"\Sjcdom01\mstudent\LHeywood\documents\A2\Computing\Comp 4 - Smail\Project\Text Files\Teacher Contact List.txt"

Upvotes: 0

BobbyTables
BobbyTables

Reputation: 4715

Well, one of your lines probably splits into an array that is shorter than you expect, and hence the index does not exist. Check the length of the array before you get the value. Maybe something like this

If TempArray.Length > 0 Then Contacts(ArrayCounter).Name = TempArray(0)
If TempArray.Length > 1 Then Contacts(ArrayCounter).Email = TempArray(1)
If TempArray.Length > 2 Then Contacts(ArrayCounter).RoomNum = TempArray(2)
If TempArray.Length > 3 Then Contacts(ArrayCounter).number1 = TempArray(3)
If TempArray.Length > 4 Then Contacts(ArrayCounter).number2 = TempArray(4)

Upvotes: 2

Don't know exactly what your TextFile contains in it. But inorder to handle the exception change the code as below

 'declare an array and use it to split line from file
    Dim TempArray() As String = Split(TextLine, ",")

    'transfer each array element into the appropriate part of the contacts stucture
     If TempArray.Length > 0 Then
    Contacts(ArrayCounter).Name = TempArray(0)
    *Contacts(ArrayCounter).Email = TempArray(1)*
    Contacts(ArrayCounter).RoomNum = TempArray(2)
    Contacts(ArrayCounter).number1 = TempArray(3)
    Contacts(ArrayCounter).number2 = TempArray(4)
    End If
    'empty string before reading next line from file
    TextLine = ""

Upvotes: 0

Related Questions