2015-03-31 25 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 
相關問題