By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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

"Morten Snedker" <morten_spammenot_ATdbconsult.dk> wrote in message
news:c1********************************@4ax.com...
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

Nov 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.