473,505 Members | 13,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1459
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
29596
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
3611
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
3568
by: cyberdave | last post by:
Someone please help me! I have a template class like this: -------------------------------------------------- template<typename T> class List { public:
3
2081
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
2492
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
2385
by: chriscorbell | last post by:
I'm curious about what appears to be a restriction on using an STL container inside a user-defined template, esp. using an iterator to such a container. It's not clear to me if this is a general...
3
5754
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
1253
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
2229
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
3009
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
7213
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5026
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1526
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.