Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

The .NET Framework introduces Enumerators which can be used to iterate through the collections. Suppose that you have a Group which has SubGroups and each SubGroup has some items. You can iterate the Parent(Group), Child(SubGroup) using Enumerations. Check out the code below:

Group.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Something
{
    
public class Group : IEnumerable
    {
        
private string _name;
        
private List<SubGroup> _subGroupList = new List<SubGroup>();
        
        
public string Name
        {
            
get return _name; }
            
set { _name = value; }
        }

        
public void AddSubGroup(string name)
        {
            SubGroup subGroup = 
new SubGroup(name);
            _subGroupList.Add(subGroup); 
        }

        
public int Count
        {
            
get return _subGroupList.Count; } 
        }
 

        
public IEnumerator GetEnumerator()
        {
            
for (int index = 0; index < Count; index++)
            {
                yield 
return _subGroupList[index]; 
            }
        }


    }
}

SubGroup.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Something
{
    
public class SubGroup : IEnumerable
    {
        
private string _name;

        
private List<Item> _itemList = new List<Item>(); 

        
public string Name
        {
            
get return _name; }
            
set { _name = value; } 
        }

        
public SubGroup(string name)
        {
            _name = name; 
        }

        
public void AddItem(string name)
        {
            Item item = 
new Item(name);
            _itemList.Add(item); 
        }

        
public int Count
        {
            
get return _itemList.Count; } 
        }

        
public IEnumerator GetEnumerator()
        {
            
for (int index = 0; index < Count; index++)
            {
                yield 
return _itemList[index]; 
            }
        }


    }
}

Item.cs:

using System;
using System.Collections.Generic;
using System.Text;

namespace Something
{
    
public class Item
    {
        
private string _name;

        
public string Name
        {
            
get return _name; }
            
set { _name = value; } 
        }

        
public Item(string name)
        {
            _name = name; 
        }
    }
}

And now you can iterate over the collections using the following code:

using System;
using System.Collections.Generic;
using System.Text;

namespace Something
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Group group = 
new Group();
            group.AddSubGroup("SubGroup1");
            group.AddSubGroup("SubGroup2");

            
// Add two new items in each group
            
foreach (SubGroup sg in group)
            {
                sg.AddItem("Item1");
                sg.AddItem("Item2"); 
            }

            
foreach (SubGroup subGroup in group)
            {
                Console.WriteLine(subGroup.Name);

                
foreach (Item item in subGroup)
                {
                    Console.WriteLine(item.Name); 
                }
            }

        }
    }
}


 

powered by IMHO 1.3

Posted on Saturday, March 18, 2006 10:58 PM | Back to top


Comments on this post: Enumerators to iterate through the collections

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net