Click here to Skip to main content
15,896,730 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.7K   2.9K   121  
Creating custom collections from IEnumerable(T) to IDictionary(T) and everything in between!
Friend Class AlphabetEnumerator
	Implements IEnumerator(Of String)

	Private _alphabet As IEnumerable(Of String)
	Private _position As Integer
	Private _max As Integer

	Public Sub New(ByVal alphabet As IEnumerable(Of String))
		MyBase.New()
		_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
	End Sub

	''' <summary>
	''' Returns the item at the current index in a foreach loop.
	''' </summary>
	''' <remarks>Is only called when MoveNext returned True.</remarks>
	Public ReadOnly Property Current As String Implements System.Collections.Generic.IEnumerator(Of String).Current
		Get
			Return _alphabet(_position)
		End Get
	End Property

	''' <summary>
	''' This is the non-generic version of Current.
	''' </summary>
	Private ReadOnly Property Current1 As Object Implements System.Collections.IEnumerator.Current
		Get
			Return Me.Current
		End Get
	End Property

	''' <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 Function MoveNext() As Boolean Implements System.Collections.IEnumerator.MoveNext
		If _position < _max Then
			_position += 1
			Return True
		End If
		Return False
	End Function

	''' <summary>
	''' For interoperability with COM. Not supported/implemented.
	''' </summary>
	Private Sub Reset() Implements System.Collections.IEnumerator.Reset
		Throw New NotImplementedException
	End Sub

	''' <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 Sub Dispose() Implements IDisposable.Dispose
		' Do not throw an exception here.
		' This method is called after every loop!
	End Sub

End Class

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