SIGN UP MEMBER LOGIN:    
ARTICLE

Custom enumerator class in C#

Posted by Rahul Ray Articles | C# Language June 03, 2010
In this article I will show an example of a custom enumerator class.
Reader Level:

Below class is an example of a custom enumerator class. It is a generic class used to contain details on of the related object. So one can use this class in for each loop and for loop to retrieve data. It can be used in the place of data set .Because in this class you can store objects of a utility class. So each object can contain different value.

Gettrxcardenumarator.cs 

namespace  cardenumarator
{
    #region[Directive]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    #endregion[Directive]
    /// <summary>
    /// This class used get enumerated data for gettrxcard
    /// </summary>
    /// <typeparam name="T">Generic type</typeparam>
    public class Gettrxcardenumarator<T> : IEnumerable, IEnumerator
    {
        #region[privateData]
        private List<T> gettrxcard = new List<T>();
        private int inex = -1;
        #endregion[privateData]
        #region[Property]
        /// <summary>
        /// Gets Length
        /// </summary>
        public int Length
        {
            get { return gettrxcard.Count; }
        }
        /// <summary>
        /// Gets  gettrxcard
        /// </summary>
        object IEnumerator.Current
        {
            get
            {
                if (inex != -1)
                {
                    return gettrxcard[inex];
                }
                else
                {
                    return null;
                }
            }
        }
        /// <summary>
        /// This is the class indexing concept
        /// </summary>
        /// <param name="index">Int data type</param>
        /// <returns>Generic type</returns>
        public T this[int index]
        {
            set
            {
                gettrxcard.Add(value);
            }
            get
            {
                return gettrxcard[index];
            }
        }
        #endregion[Property]
        #region[PublicMethod]
        /// <summary>
        /// GetEnumerator function is used to iterate in foreach loop
        /// </summary>
        /// <returns>IEnumerator data type</returns>
        public IEnumerator GetEnumerator()
        {
            return this;
        }
        /// <summary>
        /// It i used to get the next record
        /// </summary>
        /// <returns>A<see cref="System.Boolean"/> value returing true or false </returns>
        public bool MoveNext()
        {
            if (inex == gettrxcard.Count - 1)
            {
                Reset();
                return false;
            }
            inex++;
            return true;
        }
        /// <summary>
        /// Used to reset the value
        /// </summary>
        public void Reset()
        {
            this.inex = -1;
        }
        #endregion[PublicMethod]
    }
}

Below code is use to define how to use this enumerator class 

UseEnumurator.cs

namespace
  cardenumarator
{
     Public Class  StudentDetail
     {
         #region[PrivateField]
          Private string  name;
          Private  string age;
          Private  string  sex;
         #endregion[PrivateField]
         #region[Poperty]
         /// <summary>
         /// Gets  or sets Name
         /// </summary>
         Public string Name
         {
              get{return name;}
              set{name=value;}
         }
          /// <summary>
         /// Gets  or sets  Age
         /// </summary>
          Public string Age
         {
              get{return age;}
              set{age=value;}
         }
         /// <summary>
         /// Gets  or sets  Sex
         /// </summary>
         Public string Sex
         {
              get{return sex;}
              set{sex=value;}
         }
        #endregion{Poperty]
      } 
      Public Class UseEnumurator
     {
          /// <summary>
        /// create the  instance of the TransactionRetrieveDAL class from being created.
        /// </summary>
            public  UseEnumurator()
            {
            } 
           public   Gettrxcardenumarator<StudentDetail> GetUsersDetail()
           {
               StudentDetail  student1 = new  StudentDetail();
               student1.Name="Rahul";
               student1.Age="12";
               student1. sex="M";
               StudentDetail  student2 = new  StudentDetail();
               student2.Name="Rahul";
               student2.Age="12";
               student2. sex="M";
               Gettrxcardenumarator<StudentDetail> StudentDetail = new Gettrxcardenumarator<StudentDetail>();
               StudentDetail[0]= student1;
               StudentDetail[0]= student2;
             }
             void Main()
             {
                    Gettrxcardenumarator<StudentDetail> StudentDetail=GetUsersDetail();
                    foreach( StudentDetail student  in    StudentDetail.)
                    {
                         console.writeLine(student.Name);
                         console.writeLine(student.Age);
                         console.writeLine(student.Sex);
                         console.readLine();
                     }
            }
     }

Login to add your contents and source code to this article
Article Extensions
Contents added by somera bashir on Nov 25, 2011
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor