是什么导致'索引超出了数组的范围'错误?

时间:2014-02-27 20:07:41

标签: vb.net csv foreach

导致'索引超出数组范围'的原因是什么?它不能是我的文件,不是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

非常感谢所有帮助,

2 个答案:

答案 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个元素。如果您尝试选择ANYTHING但索引 0 ,则会抛出异常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

答案 1 :(得分:0)

在您的第Dim item() As String = line.Split(","c)行中,无法保证存在正确数量的元素。其中一行可能缺少逗号或文档中的空白尾随行。您可能希望添加If item.Length >= 7并跳过没有正确行数的行。另外,请记住,与VB6不同,.Net中的数组基于0而不是基于1,因此请确保第(6)项是您认为的值。