views:

149

answers:

2

I have not until now tried to use a foreach clause in a generic list. The compile error I get is:

foreach statement cannot operate on variables of type 'DMS.OrderNodeList' because 'DMS.OrderNodeList' does not contain a public definition for 'GetEnumerator'

Any suggestions what to do next?

Thanks,

+1  A: 

You need to implement the IEnumerable interface in your custom collection (it is not a generic list).

klausbyskov
If it exposes IEnumerable a .ToList().ForEach() would be an option.... I think you mean he has to explicitly implement IEnumerable<>, no?
Sky Sanders
@Sky The irony being that the IEnumerable<T> interface does not actually have ForEach() as a member. Alas, the original post uses the word "generic" in a vague fashion, so we don't know if the list needs to be generic (IEnumerable<T>) or if it's "generic" in that it holds misc. things.If he just needs ForEach(), then IEnumerable is what he needs, IEnumerable<T> doesn't force an implementation.
bakasan
I hear ya. Guess we will have to wait for the beta release of DMS.OrderNodeList to find out.
Sky Sanders
@Sky I've signed up for the invite-only beta of DMS.OrderNodeList on their secret Facebook page. Yay! Can't wait.
bzlm
+3  A: 

Tell your OrderNodeList class to extend Collection<OrderNode>, then you'll get this enumerator in for you (and probably much of the logic you've created manually to manage the collection will now be implemented for you.)

This is assuming that your OrderNodeList holds a collection of OrderNode objects.

Andy Shellam
Collection of Lists, are you sure? Should it not be extending Collection<OrderNode>
Mark Dickinson
Argh of course!
Andy Shellam