2015-03-31 86 views
0

我试图从文本文件显示信息到多行文本框中。我运行代码,但系统显示错误消息'索引超出了数组的范围'。没有明显的错误消息,我似乎无法操纵代码来摆脱此问题。看一看:我不断收到和错误消息'索引超出了数组的范围'

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 

*所包含的文本是系统表示它位于数组边界之外的地方。

+0

该数组有7个元素。文本文件有多大的可能性?好吧,我想象的是大概100%。改为使用“List(Of Details)”。当Split()产生一个太小的数组时,你必须跳过空行并大叫。使用TempArray.Length来检查。 – 2015-03-31 10:00:59

+0

该文本文件目前有三行,每行有5项数据 – LMH 2015-03-31 11:44:42

回答

0

不清楚你的TextFile包含了什么。但序处理异常改变代码如下

'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

好了,您的一条线路可能分裂成一个数组,它是比预期更短,因此该指数不存在。在获取值之前检查数组的长度。也许是这样的

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) 
+0

A.Smail,asmail @ stbedes.uk.net,A031,074276,58952这是什么元素? – LMH 2015-03-31 11:51:54

+0

什么元素是什么? – TheWanderingMind 2015-03-31 12:07:28

+0

无论每行有多少个元素,它都应该正常运行。也许你的文件有一行在最后一行结束 - 这使最后一行为空,只有一个元素长 – BobbyTables 2015-03-31 12:14:26

0

这将是有益的,如果您提供的文件还的内容:

“\ Sjcdom01 \ mstudent \ LHeywood \文件\ A2 \计算\比较4 - 斯迈尔\项目\ Text Files \教师联系List.txt“

+0

以下是我的文本文件中的一行示例 – LMH 2015-03-31 11:49:01

+0

A.Smail,asmail @ stbedes.uk.net,A031,074276,58952 – LMH 2015-03-31 11:49:52

0

我认为你应该检查行是否为空,因为0作为一个空字符串可以没有错误,但项目1将抛出'索引超出阵列的范围'在LoadTeachContacts Sub

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