Michael Dawson

For example when you need to go through a folder structure I just have a function call itself and do the same for each sub folder before handeling files. I know this is a bad way of doing it, but what is the best way of achieving this effect without self referencing functions

Any help or input will be appreciated.

Thank you :)



Re: Visual C# General Alternative to function calling itself

Paul Louth

There's nothing wrong with that.  It's called recursion.  However if you insist on doing it yourself then you can use a Stack to save your state, but it's far simpler to use the languages function stack.  The only gotcha is if you go really deep, and create a lot of data for each level.





Re: Visual C# General Alternative to function calling itself

nippysaurus

Is this fine because of the managed framework Like would it be ok to do this in C/C++ as well



Re: Visual C# General Alternative to function calling itself

Paul Louth

Perfectly fine on both.  Just depends on how much data you're sticking on the function stack for each recursion level.  So the idea is to limit the amount of local variables you create in the method.  If you go too deep, then you may get a StackOverflow exception, but if you're working with folders then you should be fine.



Re: Visual C# General Alternative to function calling itself

Paul Louth

And to further that, I find recursive functions to be a very elegant solution to traversal of tree structures. Certainly more elegant than managing the stack yourself!