
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Creating Custom Enumerators
|
329
public void ReadNestedObjects(Set topLevelGroup)
{
Console.WriteLine("topLevelGroup.Count: " + topLevelGroup.Count);
// Outer foreach to iterate over all SubGroup objects
// in the topLevelGroup object
foreach (SubGroup SG in topLevelGroup)
{
Console.WriteLine("\tSG.SubGroupName: " + SG.SubGroupName);
Console.WriteLine("\tSG.Count: " + SG.Count);
// Inner foreach to iterate over all Item objects
// in the current SubGroup object
foreach (Item i in SG)
{
Console.WriteLine("\t\ti.ItemName: " + i.ItemName);
Console.WriteLine("\t\ti.ItemLocation: " + i.ItemLocation);
}
}
}
This method displays the following:
topLevelGroup.Count: 2
SG.SubGroupName: sg1
SG.Count: 2
I.ItemName: item1
I.ItemLocation: 100
I.ItemName: item2
I.ItemLocation: 200
SG.SubGroupName: sg2
SG.Count: 2
I.ItemName: item1
I.ItemLocation: 100
I.ItemName: item2
I.ItemLocation: 200
As you see here, the outer foreach loop is used to iterate over all SubGroup objects
that are stored in the top-level
Group object. The inner foreach loop is used to iterate
over all
Item objects that are stored in the current SubGroup object.
See Also
See the “Iterators,” “yield,” “IEnumerator Interface,” and “IEnumerable Interface”
topics in the MSDN documentation.
6.2 Creating Custom Enumerators
Problem
You need ...