473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
1 1264
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
106
by: A | last post by:
Hi, I have always been taught to use an inialization list for initialising data members of a class. I realize that initialsizing primitives and pointers use an inialization list is exactly the...
12
by: Steven T. Hatton | last post by:
This is something I've been looking at because it is central to a currently broken part of the KDevelop new application wizard. I'm not complaining about it being broken, It's a CVS images. ...
6
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
28
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions!...
7
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about...
4
by: Chris F Clark | last post by:
Please excuse the length of this post, I am unfortunately long-winded, and don't know how to make my postings more brief. I have a C++ class library (and application generator, called Yacc++(r)...
6
by: Grok | last post by:
In writing a class library, one of the classes should only ever be instantiated once, and its object should be accessible to every other class in the library. How can I do that? To provide some...
12
by: Gordon | last post by:
I want to provide a set of static functions in a superclass that work with class constants defined in a decendant of that class. Unfortunately I've run into a snag with this idea. Example: ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.