有谁知道" mscorlib:索引超出范围"在我的vbscript代码中意味着什么?

时间:2016-01-24 04:41:25

标签: arraylist vbscript mscorlib

这是我的代码:

dim myArrayList

function addName

    Wscript.StdOut.WriteLine "What is your Quarterback's name?"
    n = Wscript.StdIn.ReadLine

    Wscript.StdOut.WriteLine "Attempts: "
    a = Wscript.StdIn.ReadLine

    Wscript.StdOut.WriteLine "Completions: "
    c = Wscript.StdIn.ReadLine

    Wscript.StdOut.WriteLine "Yards: "
    y = Wscript.StdIn.ReadLine

    Wscript.StdOut.WriteLine "Touchdowns: "
    t = Wscript.StdIn.ReadLine

    Wscript.StdOut.WriteLine "Interceptions: "
    i = Wscript.StdIn.ReadLine


Set myArrayList = CreateObject( "System.Collections.ArrayList" )
    myArrayList.Add n
    myArrayList.Add a
    myArrayList.Add c
    myArrayList.Add y 
    myArrayList.Add t
    myArrayList.Add i

end function 

addname()

function show
    for i = 1 to myArrayList.count
        Wscript.StdOut.WriteLine myArrayList(i)
    next
end function

show()

我收到一条错误, " mscorlib:索引超出范围。必须是非负数且必须小于集合的大小。参数名称:索引"

我不知道问题所在 任何人都可以帮我解决这个问题吗?感谢。

1 个答案:

答案 0 :(得分:5)

.NET System.Collections.ArrayList类使用从0开始的索引:第一个元素位于索引0,最后一个元素位于索引Count - 1For循环的最后一次迭代会导致错误,因为它会尝试访问索引为Count的元素,而该元素不存在。

修改您的For循环,使其从0到myArrayList.Count - 1计数:

For i = 0 To myArrayList.Count - 1
    WScript.StdOut.WriteLine myArrayList(i)
Next