2016-01-24 97 views
0

这里是我的代码:有没有人知道什么“mscorlib:索引超出范围”意味着在我的VBScript代码?

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程序:索引超出范围必须为非负且必须小于集合参数名称的大小。:索引“

我不知道问题是什么 任何人都可以帮我解决这个问题吗?谢谢。

回答

4

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

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

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

这是一个非常明确的答复。谢谢 –

+0

不客气。 –

相关问题