2014-02-27 94 views
-1

是什麼導致'索引超出了數組的界限'錯誤?它不能是我的文件,defianetly不是。下面是我的代碼:什麼導致'索引超出了數組的界限'錯誤?

Sub pupiltest() 

    Dim exitt As String = Console.ReadLine 
    Do 
     If IsNumeric(exitt) Then 
      Exit Do 
     Else 
      'error message 
     End If 
    Loop 

    Select Case exitt 
     Case 1 
     Case 2 
     Case 3 
    End Select 

    Do 
     If exitt = 1 Then 
      pupilmenu() 
     ElseIf exitt = 3 Then 
      Exit Do 
     End If 
    Loop 

    Dim score As Integer 
    Dim word As String 
    Dim totalscore As Integer = 0 

    'If DatePart(DateInterval.Weekday, Today) = 5 Then 
    'Else 
    ' Console.WriteLine("You are only allowed to take the test on Friday unless you missed it") 
    ' pupiltest() 
    'End If 

    Dim founditem() As String = Nothing 
    For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv") 
     Dim item() As String = line.Split(","c) 
     founditem = item 
    Next 

    Dim stdntfname As String = founditem(3) 
    Dim stdntsname As String = founditem(4) 
    Dim stdntyear As String = founditem(5) 
    Console.Clear() 

    If founditem IsNot Nothing Then 
     Do 
      If stdntyear = founditem(5) And daytoday = founditem(6) Then 
       Exit Do 
      ElseIf daytoday <> founditem(6) Then 
       Console.WriteLine("Sorry you are not allowed to do this test today. Test available on " & item(6).Substring(0, 3) & "/" & item(6).Substring(3, 6) & "/" & item(6).Substring(6, 9)) 
       Threading.Thread.Sleep(2500) 
       pupiltest() 
      ElseIf stdntyear <> founditem(5) Then 
       Console.WriteLine("Year not found, please contact the system analysts") 
       Threading.Thread.Sleep(2500) 
       pupiltest() 
      End If 
     Loop 
    End If 

    For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\testtests.csv") 
     Dim item() As String = line.Split(","c) 
     Dim mine As String = String.Join(",", item(2), item(3), item(4), item(5), item(6)) 
     For i As Integer = 1 To 10 
      Console.WriteLine(i.ToString & "." & item(1)) 
      Console.Write("Please enter the word: ") 
      word = Console.ReadLine 
      If word = Nothing Or word <> item(0) Then 
       score += 0 
      ElseIf word = item(0) Then 
       score += 2 
      ElseIf word = mine Then 
       score += 1 
      End If 
     Next 

     If score > 15 Then 
      Console.WriteLine("Well done! Your score is" & score & "/20") 
     ElseIf score > 10 Then 
      Console.WriteLine("Your score is" & score & "/20") 
     ElseIf score Then 
     End If 
    Next 

    Using sw As New StreamWriter("F:\Computing\Spelling Bee\stdntscores", True) 
     sw.Write(stdntfname, stdntsname, stdntyear, score, daytoday, item(7)) 
     Try 
     Catch ex As Exception 
      MsgBox("Error accessing designated file") 
     End Try 
    End Using 



    End 
End Sub 

所有幫助表示高度讚賞,

+1

你能告訴我們更多的代碼,可能是這個特定的代碼片段的整個子程序。 – Dayan

+0

你用那個item()數組或foundItem()數組做什麼? – LarsTech

+0

異常告訴你究竟哪一行導致了問題。哪一個? –

回答

0

您會不斷更換foundItem陣列,當你做founditem = item

Dim founditem() As String = Nothing 
    For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv") 
     Dim item() As String = line.Split(","c) 
     founditem = item 
    Next 

此外,您使用(= )的賦值操作而不是(==)關係運算符來比較。請參閱this article以瞭解兩者之間的區別。

取而代之的是:If stdntyear = founditem(5) And daytoday = founditem(6) Then

使用此:If (stdntyear == founditem(5)) And (daytoday == founditem(6)) Then

現在回到你的主要錯誤。每次迭代時(覆蓋以前的內容),您都會繼續將item陣列指定爲founditem。在Iteration的末尾,您將只剩下CSV中的最後一個條目......換句話說,founditem將只包含1個元素。如果你嘗試挑選任何東西,但索引,它將拋出異常index was outside the bounds of the array

因此,當您嘗試執行以下操作時,它會引發異常。

Dim stdntfname As String = founditem(3) 'index 3 does not exist! 

要修復它做了如下修改:

Dim founditem() As String = Nothing 
For Each line As String In File.ReadAllLines("F:\Computing\Spelling Bee\stdnt&staffdtls.csv") 
    'NOTE: Make sure you know exactly how many columns your csv has or whatever column 
    '  you wish to access. 
    Dim item() As String = line.Split(","c) 
    founditem(0) = item(0) 'Assign item index 0 to index 0 of founditem... 
    founditem(1) = item(1) 
    founditem(2) = item(2) 
    founditem(3) = item(3) 
    founditem(4) = item(4) 
    founditem(5) = item(5) 
    founditem(6) = item(6) 
Next 

有關如何使用VB.NET Arrays工作訪問這個網站更多的幫助:http://www.dotnetperls.com/array-vbnet

+0

對不起,應該在founditem = item – SkyTrees

+0

之後有一個'Exit For',但它仍然會拋出錯誤 – SkyTrees

+0

現在我收到這個消息,「對象引用未設置爲對象的實例。」 – SkyTrees

0

在你行Dim item() As String = line.Split(","c)有不能保證存在正確數量的元素。其中一行可能缺少逗號,或者是文檔中的空行尾。您可能想要添加一個If item.Length >= 7並跳過沒有適當行數的行。另外,請記住,與VB6不同,.Net中的數組基於0而不是1,所以請確保item(6)是您認爲它的值。