Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I change my foreach loop to for loop. I need to measure the performance between them.

thanks

What I have tried:

public void ClearAllTextBoxes(Control controls)
     {
         foreach (Control control in controls.Controls)
         {
             if (control is TextBox)
             {
                 ((TextBox)control).Clear();
             }
             if (control.HasChildren)
             {
                 ClearAllTextBoxes(control);
             }
         }
     }
Posted
Updated 20-Jun-18 7:24am
Comments
BillWoodruff 20-Jun-18 15:09pm    
"I need to measure the performance between them."

What does this mean ? Between what ?

(Not tested)
public void ClearAllTextBoxes(Control controls)
     {
         int n;
         for (n=0; n<controls.Controls.Count; ++n)
         {
             Control control = control.Controls[n];
             if (control is TextBox)
             {
                 ((TextBox)control).Clear();
             }
             if (control.HasChildren)
             {
                 ClearAllTextBoxes(control);
             }
         }
     }
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 8:57am    
5
CPallini 20-Jun-18 11:01am    
Thank you, Maciej!
Something along the line of:
for(int i = 0; i < controls.Controls.Count; i++){
   if (controls.Controls[i] is TextBox)
             {
                 ((TextBox)controls.Controls[i]).Clear();
             }
             if (controls.Controls[i].HasChildren)
             {
                 ClearAllTextBoxes(controls.Controls[i]);
             }
}


This is documented and is basic .Net knowledge.
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 8:57am    
5
Member 10877592 20-Jun-18 9:46am    
thanks
This version uses the most modern form of C#.

public void ClearAllTextBoxes(Control controls)
{
    for (int n = 0; n < controls.Controls.Count; n++)
    {
         var control = control.Controls[n];
         if (control is TextBox textBox)
         {
             textBox.Clear();
         }
         if (control.HasChildren)
         {
             ClearAllTextBoxes(control);
         }
    }
}
 
Share this answer
 
Comments
Maciej Los 20-Jun-18 8:58am    
5
I prefer to handle such recursive manipulations with extension methods.

Use examples:
this.ApplyToControls<TextBox>(tbx => tbx.Clear());

var tbxs = this.GetAllControlsOfType<TextBox>();

tbxs.ApplyToControlCollection<TextBox>(tbx => tbx.Clear());

tbxs.ApplyToControlCollection<TextBox>(tbx => tbx.Text = "default");
Code:
public static class ControlExtensions
{
    public static IEnumerable<T> GetAllControlsOfType<T>(this Control cntrl)
        where T : Control
    {
        var stack = new Stack<Control>(cntrl.Controls.Cast<Control>());

        while (stack.Any())
        {
            Control nextControl = stack.Pop();

            if (nextControl.HasChildren)
            {
                foreach (Control childControl in nextControl.Controls)
                {
                    stack.Push(childControl);
                }
            }

            if(nextControl is T) yield return nextControl as T;
        }
    }

    public static void ApplyToControlCollection<T>(this IEnumerable<T> instances, Action<T> action)
    {
        foreach (T instance in instances)
        {
            action(instance);
        }
    }

    public static void ApplyToControls<T>(this Control cntrl, Action<T> action)
    {
        foreach (T cinstance in cntrl.Controls.OfType<T>())
        {
            action(cinstance);
        }

        if (cntrl.HasChildren)
        {
            foreach (Control childcntrl in cntrl.Controls)
            {
                ApplyToControls<T>(childcntrl, action);
            }       
        }
    }
}
 
Share this answer
 
for(int i = 0; i < controls.Controls.Count; i++)
{
var control = control.Controls[i]
if (control is TextBox)
{
((TextBox)control).Clear();
}
if (control.HasChildren)
{
ClearAllTextBoxes(control);
}
}

For learn Deeply watch here:See Sharp
 
Share this answer
 

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