Make class return more than one result (class enumerating folders)

M

Morten Snedker

I've made myself a class for enumerating folders. But I'm a bit
confused how to use it. It looks like this:

'--Class begin
Imports System.IO
Public Class ListFolders

Private _folder As String
Private _listSubFolders As Boolean

Sub New(ByVal Folder As String, ByVal ListSubDirs As Boolean)
_folder = Folder
_listSubFolders = ListSubDirs
End Sub
Public ReadOnly Property GetFolder()
Get
Return _folder
End Get
End Property

Private Sub ProcessDirectory(ByVal targetDirectory As String,
ByVal getSubFolders As Boolean)

Try
Dim fileEntries As String() =
Directory.GetFiles(targetDirectory)
Dim subdirectoryEntries As String() =
Directory.GetDirectories(targetDirectory)

Dim fileName As String, folderName As String, subdirectory
As String


If getSubFolders = True Then
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory, True)
Next subdirectory
End If

For Each folderName In subdirectoryEntries
_folder = folderName
Next folderName

Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
End Class
'--Class end

My call looks like:

Dim lf As ListFolders
lf = New ListFolders("c:\", False)

...but then what? How do I make my class keep returning each folder? Or
should my class return an array...or..or?

I'm bit of a newbie to .net, so any comments on the code in general is
also more than welcomed (except for missing error handling which i
know is missing).

Thanks in advance for any help! :)

Regards /Morten
 
R

Robin Tucker

You are using a recursive function. It won't do any harm to pass the
collection into the function and continually add to it, something like this:

Dim theItems as new ArrayList

ProcessDirectory ( "C:\", True, theItems )

.....
For Each theItem as String in theItems

......

Next




Private Sub ProcessDirectory(ByVal targetDirectory As String, ByVal
getSubFolders As Boolean, ByVal theResults as ArrayList)


......
...... Add directories to theResults as you go around....
......


End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top