Click here to Skip to main content
15,880,299 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I know the easiest way is to use RECURSION, but I hate recursion, don't want to use it at all, just for the way it works!
In fact, all recursive functions can be able to converted to normal functions using a misc of LOOP statements. But I really see that, some problem will be more complex if we don't use recursion (such as Browsing all nodes in a binary tree?). And my problem with adding all controls on a form to a collection or an arraylist is another case, too simple if using Recursion, but with the solution I am using (don't use recursiom) I feel it is really hard to do if the form has so many controls, this contains that and this contains that (many layers of child - parent relationship). My form stops at only 4 such layers but It needs at least 4 foreaches to browse all (the more foreaches will be needed if using TabControls). Could you please tell me more about "RECURSION AND THE REPLACEMENT" or "THE IMPORTANCE OF RECURSION" and give me the solution to solve my problem without using RECURSION the most simply and easily?
Thank you so much!
Plus, can you give me some reason to make me love recursion?
Ah, Do developers use it in coding and publishing libraries like .NET library?
Thank you!
Posted
Updated 13-Apr-11 0:18am
v2

Replacing recursion with anything else is not easy, that is why it is such a powerful technique.
Read this[^] first, then come back here and read on.

To do a recursive function without recursion means remembering where you are, and coming back to exactly that point later. This is not simple at all, when you do not know the potential depth of the lists you are traversing.

Much better to do it with recursion: it is simple to set up and easy to understand.
"Yeah, too simple and easy to understand, powerful, just in some cases we can't replace it if we like simplicity, isn't it?
So what about my problem? Should I use recursion? Thank you so much!"


Yes. Think about it: a control contains a list of controls, so it is a naturally recursive structure. To process it without treating it as such is hugely complicating things beyond what is necessary.
 
Share this answer
 
v2
Comments
[no name] 13-Apr-11 6:23am    
Yeah, too simple and easy to understand, powerful, just in some cases we can't replace it if we like simplicity, isn't it?
So what about my problem? Should I use recursion? Thank you so much!
OriginalGriff 13-Apr-11 6:33am    
Answer updated
Sergey Alexandrovich Kryukov 13-Apr-11 6:45am    
In certain cases -- yes, of course. Please see my Answer as well.

My 5 for Griff's Answer!
--SA
[no name] 13-Apr-11 8:09am    
Thank you all! I will love it! In fact, some teachers of mine grave the idea like "Recursion is good but ... (anything related to the way it works including memory overflow)" in my mind. Now I will free it!
Thank you so much!
Maybe this will help?

static Controls[] GetControlsArray(Control container)
{
  List<control> controlsList = new List<control>;
  TabControl tabCtrl;

  foreach(Control cont in container.Controls)
  {
     controlsList.Add(cont);
     switch(cont.GetType().ToString())
     {
         //TabControl 
         case "System.Windows.Forms.TabControl":
           tabCtrl = (TabControl) cont;
           foreach(TabPage tp in tabCtrl.TabPages)
           {
              controlsList.Add(GetControlsArray(tp));
           }

         //handle other non-typical controls in the same manner

         //others
         default:
           controlsList.Add(GetControlsArray(cont));
           break;
     }
  }

  //TODO: some sorting perhaps?
  return controlsList.ToArray();
}</control></control>
 
Share this answer
 
v3
Comments
[no name] 13-Apr-11 7:49am    
Wow! I only see it is exactly what called "RECURSION", why do you say it is not too recursive?
Can your code work well if my form has a tabcontrol? Yeah, tabControl1 is in Form1.Controls, but there is no Controls as a property of tabControl1, instead, we have TabPages, each tabPage in TabPages will contain Controls. So I think, your code should be added in an If statement to check if the Control is TabControl. Thank you for a good example on using List.
Oshtri Deka 13-Apr-11 8:34am    
Use recursion, resistance is futile. :)
If you use this method, all that recursion you dislike is hidden from your eyes. ;)
I would strongly recommend you to use recursion.
But, if you really hate it, here is another (poor, slow) approach: Reflection
Just enumerate the fields of your Form class and filter out those which are not Controls.

Note: Dynamically added Controls are not covered by this technique.

P.S. It may be a good exercise to learn about reflection.
 
Share this answer
 
Comments
[no name] 13-Apr-11 7:38am    
Thank you! You have given me an expensive key word "Reflection"! To know more, I should search for it, uhm, it may be better if you post some code using it here! Anyway, thank you so much!
Toli Cuturicu 13-Apr-11 7:56am    
Just check the classes in System.Reflection namespace.
Stop using ArrayList! Use System.Collections.Generic.List instead, consider other collections from System.Collections.Generic. Non-generic version (not counting the specialized) should be considered obsolete since introduction of generics in .NET Framework 2.0.

Non-generic container invites more bugs due to type casting, which is also inconvenient. There is no need to use them anymore.

Now, about recursion. What's so special about recursion. It can be abused or used reasonably, just like anything else. Just consider recursion depth and the volume of stack frames. The logic is more important.

"I have recursion" leads your nowhere. There are cases when you cannot avoid recursion. Control traversal is one of such cases. Controls relationship is a tree — enough said.

—SA
 
Share this answer
 
v2
Comments
[no name] 13-Apr-11 8:02am    
Thank you so much for recommending me a new use with List, I should try it, maybe I read some too old documents which only guide me to use Array and ArrayList.
I will use it in next projects, I don't like to change my current code, it may produce some error and consume my time, hihi, as I said my application is intended to written once without any maintenance, I see it as a memory on how hard is it for me to build it and an ancient library with some obsolete uses like "ArrayList". Thank you!
Ah, could you please tell me if ArrayList has anything helpful and useful?
Thank you again!
All answers to this question of mine will be accepted! Please wait!
Toli Cuturicu 13-Apr-11 8:16am    
ArrayList has nothing helpful and useful compared to List<>. It is superseeded by List<>. So, just replace it.
Sergey Alexandrovich Kryukov 13-Apr-11 8:38am    
Absolutely. No use.
--SA
Sergey Alexandrovich Kryukov 13-Apr-11 8:48am    
By the way, there almost no chance that you produce any error by replacing ArrayList with generic List, as this is pretty much mechanical, but there are good chances to removed some bugs as you eliminate type down-casting. This is not my job to force you into something...
--SA
Oshtri Deka 13-Apr-11 9:24am    
I agree with ArrayList concerns and about abuse.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900