By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Generic iterating for dummies

P: n/a
Ive been using arraylists in 1.1 to return collections of custom business
objects, and thoroughly enjoyed their simple programming style.
After hearing of the advantages of generics during a 2.0 app, I decided to
try one. I changed a "GetCollectionOfObjects"-type method to return a
List<custom object>, instantiated objects,and added them to the List. Worked
great. My final step was to iterate through them, and there's the rub.
Foreach doesnt work, I get a message that I dont have a public definition for
GetEnumerator(). So I seach on it online, and Im not clear on what needs to
be done.
Any help would be greatly appreciated.
Oct 17 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

MarkAurit wrote:
Ive been using arraylists in 1.1 to return collections of custom business
objects, and thoroughly enjoyed their simple programming style.
After hearing of the advantages of generics during a 2.0 app, I decided to
try one. I changed a "GetCollectionOfObjects"-type method to return a
List<custom object>, instantiated objects,and added them to the List. Worked
great. My final step was to iterate through them, and there's the rub.
Foreach doesnt work, I get a message that I dont have a public definition for
GetEnumerator(). So I seach on it online, and Im not clear on what needs to
be done.
Any help would be greatly appreciated.
Umm, if you're using List<then it should be fine - List<has
GetEnumerator() defined. Or do you mean a custom collection object
that you're iterating across?

Using a list
List<FooClass MyList = SomeBarFunctionThatReturnsAList();

Foreach(FooClass foo in MyList)
{
doSomethingWith(foo);
}
that's how you use the generic list.

Now, if you're talking about a custom collection class that you made
yourself, then it needs to implement the IEnumerable interface, which
is 100x easier than it sounds.

To implement IEnumerable, just implement GetEnumerator, and then you
get to play with the new "Yield Return" statement (which automatically
wraps the method in an enumerator object).

So, let's say we've a class that wraps around a List<>

private List<FooClassmyList;

public IEnumerator<FooClassGetEnumerator()
{
foreach (foo in myList)
{
yield return foo;
}
}

There - that's all it takes to implement the GetEnumerator method.
Look up the yield return statement to make sense of it.

Oct 17 '06 #2

P: n/a
You don't need to use yield you can just call original GetEnumerator method.
public class MyList : IEnumerable<int>
{
private List<intlist = new List<int>();

#region IEnumerable<intMembers

public IEnumerator<intGetEnumerator()
{
return list.GetEnumerator();
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
{
return list.GetEnumerator();
}

#endregion
}
Oct 18 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.