我一直得到错误消息'索引超出了数组的范围'

时间:2015-03-31 09:58:58

标签: arrays vb.net

我正在尝试将文本文件中的信息显示到多行文本框中。我运行代码但系统显示错误消息'索引超出了数组的范围'。没有明显的错误消息,我似乎无法操纵代码来摆脱这个问题。看看:

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

*所包含的文本是系统表示它在数组范围之外的地方。

4 个答案:

答案 0 :(得分:2)

好吧,你的一行可能会拆分成一个比你预期的更短的数组,因此索引不存在。在获得值之前检查数组的长度。也许是这样的

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)

答案 1 :(得分:0)

不确切知道文本文件中包含的内容。但是为了处理异常更改代码如下

 '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 = ""

答案 2 :(得分:0)

如果您也可以提供该文件的内容,将会很有帮助:

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

答案 3 :(得分:0)

我认为您应该检查该行是否为空,因为项0将作为Null字符串可用而没有错误,但是项1将抛出'索引超出数组的范围'在LoadTeachContacts Sub中

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