I'm a bit new to enumerations at the generic level, so bear with me. I
have a foreach loop that I'm trying to navigate through, but my
enumerator isn't working properly. I'll have to post the code to give
all of you a better idea of what I am trying to accomplish.
This is in my application:
RobotParts rods = new RobotParts<Rod>(joint); //joint is an object being passed in
foreach (Rod rb in rods)
{
// Do Stuff
}
This is in my RobotParts class:
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
{
System.Collections.Generic.List<T>
childParts =
new
System.Collections.Generic.List<T>();
childParts = partToSearch.FlatPartList<T>(); // partToSearch is object of RobotPart class
return childParts.GetEnumerator();
}
This is from my RobotPart class:
internal List<T> FlatPartList<T>() //where T : RobotPart
{
List<T> part = new List<T>();
//Recurse down the children.
foreach (Socket socketName in Sockets.Values)
{
foreach (RobotPart childPart in socketName.Parts.Values)
{
//But NOT inside other robots
if (!(childPart is Robot))
{
////Add each part we found to the list
List<T> childParts = childPart.FlatPartList<T>();
foreach (T grandchildPart in childParts)
{
part.Add(grandchildPart);
}
}
}
}
return part;
}
What I want to do is be able to return each object of the specified Type of that collection that I built.

Implementing System.Collections.Generic.IEnumerator<T> GetEnumerator()
Big Andy 78
mandar17367
SaurabhKhurana
if (this.GetType() == typeof(T))
{
part.Add((T)this);
}
which I added right after "List<T> part = new List<T>();"
This allowed me to actually have values coming into my foreach loop, but now it's stuck in an infinate loop:(