Need help with my recrusive function
I am trying to write a recursive function to return a list of files within folders and subfolders of a given directory.
I get the error, Object reference not set to an instance of an object, when I do FileList.Add.
Code:
Function thisfunction(ByVal path As String, ByVal FileList As ArrayList)
Dim di As New DirectoryInfo(path)
Dim folder As DirectoryInfo
Dim folders() As DirectoryInfo
Dim file As FileInfo
Dim files() As FileInfo
folders = di.GetDirectories()
For Each folder In folders
thisfunction(path & "\" & folder.Name, FileList)
Next
files = di.GetFiles
For Each file In files
FileList.Add(path & "\" & file.Name) '-capture currentworking path & filename
Next
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Files As ArrayList
thisfunction("c:\test", Files)
MessageBox.Show(Files.Count)
End Sub
Re: Need help with my recrusive function
Where have you declared FileList?
Re: Need help with my recrusive function
You must have missed it. It's a parameter of thisfunction(). I bet this doesnt work like it did in VB6. But I would like a way to do something similar w/o having to reference some other variable defined in a more global scope.
Re: Need help with my recrusive function
My bad.
I'll go over this again.
Re: Need help with my recrusive function
Hi
You forgot to use the arraylist constructor call.
See modified code
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Files As new ArrayList
thisfunction("c:\test", Files)
MessageBox.Show(Files.Count)
End Sub
Regards
Jorge