Click here to Skip to main content
15,895,836 members
Articles / Programming Languages / Visual Basic 10

Having fun with custom collections!

Rate me:
Please Sign up or sign in to vote.
4.91/5 (71 votes)
14 Oct 2011CPOL44 min read 187.5K   2.9K   121  
Creating custom collections from IEnumerable(T) to IDictionary(T) and everything in between!
using System;
using System.Collections.Generic;
using System.Linq;

namespace Alphabet
{
	internal class WesternAlphabetEnumerator : IEnumerator<string>
	{

		private IEnumerable<string> _alphabet;
		private int _position;
		private int _max;

		public WesternAlphabetEnumerator(IEnumerable<string> alphabet)
		{
			_alphabet = alphabet;
			// The position start at -1 and represents the current index.
			// A foreach loop calls MoveNext and _position is set to 0.
			_position = -1;
			// Get the maximum number of items in the alphabet.
			// When the maximum index is reached we are at the end of the alphabet.
			_max = _alphabet.Count() - 1;
		}

		/// <summary>
		/// Returns the item at the current index in a foreach loop.
		/// </summary>
		/// <remarks>Is only called when MoveNext returned true.</remarks>
		public string Current
		{
			get { return _alphabet.ElementAt(_position); }
		}

		/// <summary>
		/// This is the non-generic version of Current.
		/// </summary>
		object System.Collections.IEnumerator.Current
		{
			get { return this.Current; }
		}

		/// <summary>
		/// Moves to the next item in a collection.
		/// If this returns true it means the index is not the last index
		/// in the collection yet. If true is returned the Current Property
		/// is called.
		/// </summary>
		/// <returns>True if the last index has not been reached yet.</returns>
		public bool MoveNext()
		{
			if (_position < _max)
			{
				_position += 1;
				return true;
			}
			return false;
		}

		/// <summary>
		/// For interoperability with COM. Not supported/implemented.
		/// </summary>
		void System.Collections.IEnumerator.Reset()
		{
			throw new NotImplementedException();
		}

		/// <summary>
		/// Not implemented. This is used in rare cases where the Enumerator
		/// opens up a database connection or files and needs to close them.
		/// </summary>
		public void Dispose()
		{
			// Do not throw an exception here.
			// This method is called after every loop!
		}

	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO JUUN Software
Netherlands Netherlands
Sander Rossel is a Microsoft certified professional developer with experience and expertise in .NET and .NET Core (C#, ASP.NET, and Entity Framework), SQL Server, Azure, Azure DevOps, JavaScript, MongoDB, and other technologies.

He is the owner of JUUN Software, a company specializing in custom software. JUUN Software uses modern, but proven technologies, such as .NET Core, Azure and Azure DevOps.

You can't miss his books on Amazon and his free e-books on Syncfusion!

He wrote a JavaScript LINQ library, arrgh.js (works in IE8+, Edge, Firefox, Chrome, and probably everything else).

Check out his prize-winning articles on CodeProject as well!

Comments and Discussions