2014-02-27 95 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)是您认为它的值。