1

为什么我不断收到此错误:索引超出了数组的范围。请帮忙。谢谢你。

 Dim list As New ListBox
 Dim dirsize As Integer
 Dim a As Integer
 Dim container(0) As String

 Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
 counter = My.Computer.FileSystem.GetFiles("C:\myfolder")
 dirsize = counter.Count

 For a = 0 To dirsize

     container(a) = a + 1
     lstItems.Items.Add(container(a))

 Next
4

1 回答 1

1

基本上container只有 1 个元素,您在子开始时会变暗。您永远不会调整容器大小,因此如果dirsize不是 0,您将收到此错误。

您应该执行以下操作:

 Dim list As New ListBox
 Dim dirsize As Integer
 Dim a As Integer
 Dim container() As String

 Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
 counter = My.Computer.FileSystem.GetFiles("C:\myfolder")
 dirsize = counter.Count

 **ReDIm container(dirsize)**
 For a = 0 To dirsize

     container(a) = a + 1
     lstItems.Items.Add(container(a))

 Next
于 2013-03-15T06:20:14.317 回答