Public Class Form1
Dim i
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
i = My.Computer.FileSystem.GetDirectories("c:\")
AddItemToList(i)
End Sub
Private Sub AddItemToList(ByVal Arr() As String)
For Each strName In Arr
ListBox1.Items.Add(strName)
Next
End Sub
End Class
i = My.Computer.FileSystem.GetDirectories("c:\"),从这句开始 i 是数组,为何不能传过去?
再说,也无法事先确定数组上限,因为不知道c盘文件夹有多少个。只能定义variant变量,让其自动变成数组(vb6是这样的)。
------解决方案--------------------
晕,GetDirectories返回的是个collection阿
------解决方案--------------------
简单些,就这样写吧。
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
For Each strName As String In My.Computer.FileSystem.GetDirectories("c:\")
ListBox1.Items.Add(strName)
Next
End Sub
End Class
或者遵循楼主的写法,可以这样改:
Public Class Form1
' Dim i '没必要放在这里,况且看起来这个变量名应该表示循环变量。
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
'i = My.Computer.FileSystem.GetDirectories("c:\")
'AddItemToList(i)
AddItemToList(My.Computer.FileSystem.GetDirectories("c:\").toArray()) '为了使用你的函数,就这样转成数组吧
End Sub
Private Sub AddItemToList(ByVal Arr() As String)
For Each strName In Arr
ListBox1.Items.Add(strName)
Next
End Sub
End Class