473,320 Members | 1,982 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.

Nested iterator problem

Hi All,

I have an issue that I can't solve with the following (cleaned up) piece
of code. What I've seen in the VS 2005 debugger is that the Files iterator
is issuing a yield on all of the files below 'dir', but that the loop in
main does nothing with them. i.e. if there were 10 files below dir, then
it would stop at the 'FileInfo info' 10 times, but would never visit the
Debug.WriteLine 10 times. I can't see what I am doing wrong here (if anything).

I know that under the hood there is a fair bit of code transformation required
to execute the iterators, so I wonder whether using an iterator from within
another iterator causes problems?

I also have the LINQ (May 2006 CTP) installed as well. Is there a chance
that this is causing a problem?

TIA

Andrew Matthews
=================================================
public class MyClass
{
private string dir = @"c:\somedir\someotherdir";

public static void main(string[] args)
{
foreach (FileInfo info in Files)
{
Debug.WriteLine(info.FullName);
}
}

public IEnumerable<FileInfo> Files
{
get
{
foreach (DirectoryInfo dir in SubDirectories(new DirectoryInfo(dir)))
{
foreach (FileInfo fileInfo in dir.GetFiles())
{
yield return fileInfo;
}
}
}
}

IEnumerable<DirectoryInfo> SubDirectories(DirectoryInfo di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
{
foreach (DirectoryInfo subDirectory in SubDirectories(directory))
{
yield return subDirectory;
}
}
}
}
May 21 '06 #1
2 1247
This works for me ...

class Program
{
private static string dirF = @"a dir with files";

public static void Main(string[] args)
{
foreach (FileInfo info in Files)
{
Console.WriteLine(info.FullName);
}

Console.ReadLine();
}

public static IEnumerable<FileInfo> Files
{
get
{
foreach (DirectoryInfo dir in SubDirectories(new
DirectoryInfo(dirF)))
{
foreach (FileInfo fileInfo in dir.GetFiles())
{
yield return fileInfo;
}
}
}
}

static IEnumerable<DirectoryInfo> SubDirectories(DirectoryInfo
di)
{
yield return di;
foreach (DirectoryInfo directory in di.GetDirectories())
{
foreach (DirectoryInfo subDirectory in
SubDirectories(directory))
{
yield return subDirectory;
}
}
}
}

regards,
Tasos

May 21 '06 #2
Andrew Matthews wrote:
Hi All,

I have an issue that I can't solve with the following (cleaned up)
piece of code. What I've seen in the VS 2005 debugger is that the Files
iterator is issuing a yield on all of the files below 'dir', but that the
loop
in main does nothing with them. i.e. if there were 10 files below dir,
then it would stop at the 'FileInfo info' 10 times, but would never visit
the Debug.WriteLine 10 times. I can't see what I am doing wrong here
(if anything).


This means that you're building with DEBUG not defined. Check your project
settings. Were you debugging a Release build by chance?

If you're looking for recursive file enumeration in particular, you might
like:

http://www.codeproject.com/csharp/Fi...Enumerator.asp

Of course, if this is just an exercise in using the new iterator support,
then carry on - you're on the right track!

-cd
May 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Joshua Beall | last post by:
Hi All, I remember reading that both nested classes and namespaces would be available in PHP5. I know that namespaces got canceled (much sadness...), however, I *thought* that nested classes...
2
by: Patrick Kowalzick | last post by:
Dear NG, I have two containers (standard library) which are nested, e.g.: std::vector< std::vector <int> > A; std::list< std::vector<int> > B; These structures where put in another class...
11
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
3
by: Chad E. Dollins | last post by:
Hello someone said that I should check this forum out for help to my c++ problems. I will admit at this point I may have really ran myself into the ground with this code that I am writing, but...
1
by: Gonzalo Aguirre | last post by:
hi!. i did this method that should perform as many iterations as Sigma element has (in the example just two: a, b), for each state (outer bucle). vector<_t_> * Mealy::to_deltaMoore() {...
3
by: utab | last post by:
Dear all, I am trying to create a look-up table for a text search and replacement class which will be used with a commercial software(MCS NASTRAN, I guess you may have heard that before ). I...
2
by: Andrew Matthews | last post by:
Hi All, I have an issue that I can't solve with the following (cleaned up) piece of code. What I've seen in the VS 2005 debugger is that the Files iterator is issuing a yield on all of the...
3
by: Gerhard Pfeiffer | last post by:
Hi, I'm trying to implement a data-structure and have an iterator for it. Now I've got a problem impleneting the operator+. I tried to isolate the problem: template<int DIM, typename Tclass...
4
by: rach | last post by:
I just started to learn C++. I copied the following code from a data structure textbook to a ".h" file and couldn't compile it. The code contains three template interfaces. One inherits another. The...
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
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...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.