locked
foreach getenumerator error RRS feed

  • Question

  • Getting a compile error "does not contain a public definition for 'GetEnumerator'". What am I missing??

    public static bool revgenHasErrors(TabControl tc, ErrorProvider errorProvider1, TextBox tbError)
            {            
                int numerrors = 0;                      
                bool isInchesTB = true, isColorTB = false;
                tbError.Text = string.Empty;
                            
                foreach (TabPage tp in tc.SelectedTab)
                {
                    foreach (Control c in tp.Controls)
                    {
                        if (c is ComboBox)
                            if (c.Text == "")                        
                            {
                                numerrors++;
                                errorProvider1.SetError(c, "Cannot leave this blank. Select a value.");
                            }
                        else
                            if (errorProvider1.GetError(c).Length > 0)
                                errorProvider1.Dispose();
                    }
                }
             }


    Wednesday, January 11, 2012 1:02 AM

Answers

  • Then you don't need a loop at all.

    Instead of the foreach line, assign the selected tab to your variable

    TabPage tp = tc.SelectedTab;

    and the rest should work as is.

    • Marked as answer by Lie You Monday, January 16, 2012 3:18 AM
    Wednesday, January 11, 2012 4:43 PM
  • Agreed. Input validation should be done only on where the user can change the values. There's no point to check at places when the current user has no access  to (e.g. the tab pages that are not active)
    • Marked as answer by Lie You Monday, January 16, 2012 3:18 AM
    Thursday, January 12, 2012 1:41 AM
    Answerer

All replies

  • TabControl.SelectedTab is not something that implements IEnumerable interface, so you cannot use foreach on it.

    You may want to use TabControl.TabPages instead.

    • Proposed as answer by Tyler_A Wednesday, January 11, 2012 2:01 AM
    Wednesday, January 11, 2012 1:31 AM
    Answerer
  • We can use foreach method only on collection

    here SelectedTab is not a collection, it will provide only one tab page so we can't apply a foreach method on this.

    Jayanti

    • Proposed as answer by Anil Sachin Wednesday, January 11, 2012 5:53 AM
    Wednesday, January 11, 2012 5:33 AM
  • Actually foreach doesn't care it's a collection, an array, generics, or some weird class that generate random objects out of it.  All it cares is that whether the class claims to be a IEnumerable, have a GetEnumerator() method that allows it to get something implements IEnumerator, then allow it to call it's Reset(), MoveNext, and Current members.

    For anything to use foreach, you should be able to rewrite the code this way and works:

                int[] anything = new int[] { 1, 3, 6, 4, 2, 5};
    
                System.Collections.IEnumerator e = anything.GetEnumerator();
    
                for (e.Reset(); e.MoveNext(); )
                {
                    Console.WriteLine(e.Current);
                }
                Console.ReadKey();
    

     EDIT: Modify the code to use anything instead of something for better effect. :P

    P.S.: Of course, the System.Collections.Generics.IEnumerable<t> and System.Collections.Generics.IEnumerator<t> will also work.

    Wednesday, January 11, 2012 6:20 AM
    Answerer
  • Hi,
      Chengo is correct . You cannot use 'For Each' on the class which doesn't implements IEnumerator Interface.

    Instead of,

     foreach (Control c in tp.Controls)

    You can use ,

    for (int i = 0; i <= tp.Controls.Count - 1; i++) {
     if ((object.ReferenceEquals(tp.Controls(i), ComboBox))) {
     }
    
    }



    If a post answers your question, please click Mark As Answer on that post and Mark as Helpful. Happy Coding...

    • Edited by Ragu.R Wednesday, January 11, 2012 6:20 AM
    Wednesday, January 11, 2012 6:20 AM
  • Ragu,

    sorry that i need to correct you, but the error isn`t thrown at this part of the code.

    The TabPage.Controls() is just a System.Windows.Forms.Control.ControlCollection which implements the IEnumerable interface.


    Hannes

    If you have got questions about this, just ask.

    In a perfect world,
    users would never enter data in the wrong form,
    files they choose to open would always exist
    and code would never have bugs.

    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    Wednesday, January 11, 2012 7:38 AM
  • Ah ok thank you all. I guess I have to figure out a different way to do this then. I only want to check for errors on the selectedtab, since there are 8 other tabs.

     

    I think this might work:

    TabPage tp = tc.SelectedTab;
    
                foreach (Control c in tp.Controls)
                {
                    if (c is ComboBox)
                    {
                         //do stuff
                     }
                     if (c is TextBox)
                     {
                        //do other stuff
                     }
                 }


    • Edited by gft_1 Wednesday, January 11, 2012 4:55 PM
    Wednesday, January 11, 2012 4:19 PM
  • Then you don't need a loop at all.

    Instead of the foreach line, assign the selected tab to your variable

    TabPage tp = tc.SelectedTab;

    and the rest should work as is.

    • Marked as answer by Lie You Monday, January 16, 2012 3:18 AM
    Wednesday, January 11, 2012 4:43 PM
  • Agreed. Input validation should be done only on where the user can change the values. There's no point to check at places when the current user has no access  to (e.g. the tab pages that are not active)
    • Marked as answer by Lie You Monday, January 16, 2012 3:18 AM
    Thursday, January 12, 2012 1:41 AM
    Answerer