<p>I need to set the height of every textbox on my form, some of which are nested within other controls.  I thought I could do something like this:</p>

<pre><code>private static IEnumerator&lt;TextBox&gt; FindTextBoxes(Control rootControl)
{
    foreach (Control control in rootControl.Controls)
    {
        if (control.Controls.Count &gt; 0)
        {
            // Recursively search for any TextBoxes within each child control
            foreach (TextBox textBox in FindTextBoxes(control))
            {
                yield return textBox;
            }
        }

        TextBox textBox2 = control as TextBox;
        if (textBox2 != null)
        {
            yield return textBox2;
        }
    }
}
</code></pre>

<p>Using it like this:</p>

<pre><code>foreach(TextBox textBox in FindTextBoxes(this))
{
    textBox.Height = height;
}
</code></pre>

<p>But of course the compiler spits its dummy, because <em>foreach</em> expects an <strong>IEnumerable</strong> rather than an <strong>IEnumerator</strong>.</p>

<p>Is there a way to do this without having to create a separate class with a <strong>GetEnumerator()</strong> method?</p>

[{'answer_id': 3316, 'author': 'David Wengier', 'author_id': 489, 'author_profile': 'https://Stackoverflow.com/users/489', 'pm_score': 5, 'selected': True, 'text': '<p>As the compiler is telling you, you need to change your return type to IEnumerable. That is how the yield return syntax works.</p>\n'}, {'answer_id': 3323, 'author': 'Yaakov Ellis', 'author_id': 51, 'author_profile': 'https://Stackoverflow.com/users/51', 'pm_score': 1, 'selected': False, 'text': '<pre><code>// Generic function that gets all child controls of a certain type, \n// returned in a List collection\nprivate static List&lt;T&gt; GetChildTextBoxes&lt;T&gt;(Control ctrl) where T : Control{\n    List&lt;T&gt; tbs = new List&lt;T&gt;();\n    foreach (Control c in ctrl.Controls) {\n        // If c is of type T, add it to the collection\n        if (c is T) { \n            tbs.Add((T)c);\n        }\n    }\n    return tbs;\n}\n\nprivate static void SetChildTextBoxesHeight(Control ctrl, int height) {\n    foreach (TextBox t in GetChildTextBoxes&lt;TextBox&gt;(ctrl)) {\n        t.Height = height;\n    }\n}\n</code></pre>\n'}, {'answer_id': 3345, 'author': 'Joseph Daigle', 'author_id': 507, 'author_profile': 'https://Stackoverflow.com/users/507', 'pm_score': 2, 'selected': False, 'text': '<p>If you return IEnumerator, it will be a different enumerator object each time call that method (acting as though you reset the enumerator on each iteration). If you return IEnumerable then a foreach can enumerate based on the method with the  yield statement.</p>\n'}, {'answer_id': 6472, 'author': 'Orion Edwards', 'author_id': 234, 'author_profile': 'https://Stackoverflow.com/users/234', 'pm_score': 3, 'selected': False, 'text': '<p>Just to clarify</p>\n\n<pre><code>private static IEnumerator&lt;TextBox&gt; FindTextBoxes(Control rootControl)\n</code></pre>\n\n<p>Changes to</p>\n\n<pre><code>private static IEnumerable&lt;TextBox&gt; FindTextBoxes(Control rootControl)\n</code></pre>\n\n<p>That should be all :-)</p>\n'}, {'answer_id': 6698655, 'author': 'supercat', 'author_id': 363751, 'author_profile': 'https://Stackoverflow.com/users/363751', 'pm_score': 0, 'selected': False, 'text': '<p>If you are given an enumerator, and need to use it in a for-each loop, you could use the following to wrap it:</p>\n\n<pre>\nstatic public class enumerationHelper\n{\n    public class enumeratorHolder&lt;T&gt;\n    {\n        private T theEnumerator;\n        public T GetEnumerator() { return theEnumerator; }\n        public enumeratorHolder(T newEnumerator) { theEnumerator = newEnumerator;}\n    }\n    static enumeratorHolder&lt;T&gt; toEnumerable&lt;T&gt;(T theEnumerator) { return new enumeratorHolder&lt;T&gt;(theEnumerator); }\n    private class IEnumeratorHolder&lt;T&gt;:IEnumerable&lt;T&gt;\n    {\n        private IEnumerator&lt;T&gt; theEnumerator;\n        public IEnumerator&lt;T&gt; GetEnumerator() { return theEnumerator; }\n        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return theEnumerator; }\n        public IEnumeratorHolder(IEnumerator&lt;T&gt; newEnumerator) { theEnumerator = newEnumerator; }\n    }\n    static IEnumerable&lt;T&gt; toEnumerable&lt;T&gt;(IEnumerator&lt;T&gt; theEnumerator) { return new IEnumeratorHolder&lt;T&gt;(theEnumerator); }\n}\n</pre>\n\n<p>The <code>toEnumerable</code> method will accept anything that <a href="/questions/tagged/c%23" class="post-tag" title="show questions tagged &#39;c#&#39;" rel="tag">c#</a> or <a href="/questions/tagged/vb" class="post-tag" title="show questions tagged &#39;vb&#39;" rel="tag">vb</a> would regard an acceptable return type from <code>GetEnumerator</code>, and return something that can be used in <code>foreach</code>.  If the parameter is an <code>IEnumerator&lt;&gt;</code> the response will be an <code>IEnumerable&lt;T&gt;</code>, though calling <code>GetEnumerator</code> on it once will likely yield bad results.</p>\n'}]
['https://Stackoverflow.com/questions/3315', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/503/']