ITERINDEX - A Code Snippet

Introduction

 
My intention for writing this article is to describe an iterIndex Code Snippet and its usage. But I will explain a bit of Code Snippets to make the article simpler.
 
What is Code Snippet
 
Visual Studio provides a feature called Code Snippets. You can use Code Snippets to type a short alias, and then expand it into a common programming construct, i.e. predefined code.
 
For example, the "for" Code Snippet creates an empty for loop, as in:
  1. for (int i = 0; i < length; i++)  
  2. { } 
And the "propfull" Code Snippet creates a full property, as in:
  1. private int myVar;  
  2. public int MyProperty  
  3. {  
  4.    get { return myVar; }  
  5.    set { myVar = value; }  

How to use Code Snippets?


To use Code Snippets through keyboard shortcut
  1. In the Visual Studio IDE, open the file that you intend to edit.
  2. In the Code Editor, place the cursor where you would like to insert the Code Snippet.
  3. Type CTRL+K, CTRL+X.
  4. Select the Code Snippet from the Code Snippet inserter and then press "Tab" or "Enter".
Alternatively, you can type the name of the Code Snippet, and then press "Tab" or "Enter".
 
To use Code Snippets through IntelliSense auto-completion
  1. In the Visual Studio IDE, open the file that you intend to edit.
  2. In the Code Editor, place the cursor where you would like to insert the Code Snippet.
  3. Type the shortcut for the Code Snippet that you want to add to your code.
  4. Type Tab, Tab to invoke the Code Snippet.
What are the benefits?
  1. Coding quickly
  2. No tension of remembering syntax.
  3. An easy approach to follow design/coding standards
  4. And the best thing is, we can define our own Code Snippets and ask the team to follow our project specific coding standards.

What is iterIndex?

 
It creates a "named" iterator and indexer pair by using a nested class. It creates a sub class to support iteration, and also provides an indexer.
 
Let's create a class Student
  1. public class Student  
  2. {  
  3.    public String Name;  
  4.    public int Age;  

And now we need a class Students, that will be a collection of Student objects. So let's define our Students Class:
  1. class Students  
  2. {  
  3.     Student[] students = new Student[100];  
  4.     int idx = -1, Tot = -1;  
  5.     //iterIndex will come here  
  6.     public void Add(Student std)  
  7.     {  
  8.         students[++Tot] = std;  
  9.     }   
  10.     public void Add(String name, int age)  
  11.     {  
  12.         students[++Tot] = new Student()  
  13.         {  
  14.             Name = name,  
  15.             Age = age  
  16.         };  
  17.     }  

Now place the cursor below the commented code //iterIndex will come here. And type iterindex and press Tab, Tab.
 
Wow!, we got predefined code generated by the IDE, here is the sample:
  1. class Students  
  2. {  
  3.     Student[] students = new Student[100];  
  4.     int idx = -1, Tot = -1;  
  5.     //iterIndex will come here   
  6.     public MyViewIterator MyView  
  7.     {  
  8.         get { return new MyViewIterator(this); }  
  9.     }   
  10.     public class MyViewIterator  
  11.     {  
  12.         readonly Students outer;   
  13.         internal MyViewIterator(Students outer)  
  14.         {  
  15.             this.outer = outer;  
  16.         }   
  17.         // TODO: provide an appropriate implementation here  
  18.         public int Length { get { return 1; } }   
  19.         public ElementType this[int index]  
  20.         {  
  21.             get  
  22.             {  
  23.                 //  
  24.                 // TODO: implement indexer here  
  25.                 //  
  26.                 // you have full access to Students privates  
  27.                 //  
  28.                 throw new NotImplementedException();  
  29.                 return default(ElementType);  
  30.             }  
  31.         }   
  32.         public System.Collections.Generic.IEnumerator GetEnumerator()  
  33.         {  
  34.             for (int i = 0; i < this.Length; i++)  
  35.             {  
  36.                 yield return this[i];  
  37.             }  
  38.         }  
  39.     }  
  40.     public void Add(Student std)  
  41.     {  
  42.         students[++Tot] = std;  
  43.     }   
  44.     public void Add(String name, int age)  
  45.     {  
  46.         students[++Tot] = new Student()  
  47.         {  
  48.             Name = name,  
  49.             Age = age  
  50.         };  
  51.     }  

Points to Notice
  • MyViewIterator Class

    Sinces it is a sub class, it will have full access to all it members, i.e private members too.
     
  • MyViewIterator Constructor

    It accepts a Students Object, and assigns it to the outer property.
     
  • Indexer inside MyViewIterator

    It also provides an indexer to access a collection item through the index.
     
  • The GetEnumerator method

    We have implemented neither IEnumerator nor IEnumerable, this will help us to iterate through the collection of Students.
     
  • And MyView property inside Students class

    It returns a new object of MyViewIterator class.
Now let's change the snippet code as in the following
  1. public class Students  
  2. {  
  3.     Student[] students = new Student[100];  
  4.     private int Tot = 0;  
  5.     public void Add(Student std)  
  6.     {  
  7.         students[Tot++] = std;  
  8.     }   
  9.     public void Add(String name, int age)  
  10.     {  
  11.         students[Tot++] = new Student()  
  12.         {  
  13.             Name = name,  
  14.             Age = age  
  15.         };  
  16.     }  
  17.     public MyViewIterator MyView  
  18.     {  
  19.         get { return new MyViewIterator(this); }  
  20.     }   
  21.     public class MyViewIterator  
  22.     {  
  23.         readonly Students outer;   
  24.         internal MyViewIterator(Students outer)  
  25.         {  
  26.             this.outer = outer;  
  27.         }   
  28.         // TODO: provide an appropriate implementation here  
  29.         public int Length { get { return outer.Tot; } }   
  30.         public Student this[int index]  
  31.         {  
  32.             get { return outer.students[index]; }  
  33.         }   
  34.         public System.Collections.Generic.IEnumerator GetEnumerator()  
  35.         {  
  36.             for (int i = 0; i < this.Length; i++)  
  37.             {  
  38.                 yield return outer.students[i];  
  39.             }  
  40.         }  
  41.     }  

Let's add some Students to our Students class
  1. class Program  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         Students s = new Students();  
  6.         s.Add("Ajay", 27);  
  7.         s.Add("Raj", 28);  
  8.     }  

Using MyViewIterator.GetEnumerator to iterate through each Student
  1. Students.MyViewIterator iterator = s.MyView;  
  2. {  
  3.     using (var student = iterator.GetEnumerator())  
  4.     {  
  5.         while (student.MoveNext())  
  6.         {  
  7.             Console.WriteLine(student.Current.Name);  
  8.         }  
  9.     }  

Using foreach to iterate through each Student
  1. foreach (Student std in s.MyView)  
  2. {  
  3.     Console.WriteLine(std.Name);  
  4. }  
Using Indexer to get the student:
  1. Student StdAjay = s.MyView[0];  
  2. Console.WriteLine(StdAjay.Name + " " + StdAjay.Age.ToString()); 
Complete Code block
  1. class Program  
  2. {  
  3.     static void Main()  
  4.     {  
  5.         Students s = new Students();  
  6.         s.Add("Ajay", 27);  
  7.         s.Add("Raj", 28);   
  8.         Students.MyViewIterator iterator = s.MyView;  
  9.         {   
  10.             using (var student = iterator.GetEnumerator())  
  11.             {  
  12.                 while (student.MoveNext())  
  13.                 {  
  14.                     Console.WriteLine(student.Current.Name);  
  15.                 }  
  16.             }  
  17.         }   
  18.         foreach (Student std in s.MyView)  
  19.         {  
  20.             Console.WriteLine(std.Name);  
  21.         }   
  22.         Student StdAjay = s.MyView[0];  
  23.         Console.WriteLine(StdAjay.Name + " " + StdAjay.Age.ToString());  
  24.     }  

So do start using Code Snippets and do coding in a smarter way.
 


Recommended Free Ebook
Similar Articles