SIGN UP MEMBER LOGIN:    
ARTICLE

Debug Troelsen's .NET books (1): IEnumerator

Posted by Zhanbo Sun Articles | C# Language December 10, 2001
This article examines code problem in chapter 4 (for C#)/chapter 5(for VB.Net) of the two .Net books.
Reader Level:

Description

Andrew Troelsen has published two .Net programming books from Apress: C# and the .NET Platform and Visual Basic .NET and the .NET Platform. You can check their websites for code download and errata. Since these are two popular books you may have already bought one. Usually you do not need to buy both of them as the second book essentially duplicates the same content as the first one. And if the first book has some bugs in its code, the second book will simply repeat it. 

This article examines code problem in chapter 4 (for C#)/chapter 5(for VB.Net) of the two .Net books. Project ObjEnum for Building a Custom Enumerator (IEnumerable and IEnumerator) does not implement Reset method correctly. Reset method, defined by IEnumerator interface, should set the enumerator to its initial position, which is before the first element in the collection. Unfortunately, the code in two books set the enumerator at the first element in the collection, which is incorrect.

Let's see the original implementation details:     

// Implementation of IEnumerator.
public bool MoveNext()
{
if(pos < carArray.Length)
{
pos++;
return true;
}
else
return false;
}
public void Reset()
{
pos = 0;
}
public object Current
{
get { return carArray[pos]; }
}

Here is the corrected implementation: 

// Implementation of IEnumerator.
bool IEnumerator.MoveNext()
{
if(pos < carArray.Length)
{
pos++;
return true;
}
else
return false;
}
void IEnumerator.Reset()
{
pos = -1;
}
object IEnumerator.Current
{
get
{
if (pos < 0 || pos >=carArray.GetLength(0))
throw (new System.InvalidOperationException());
else
return carArray[pos];
}

Login to add your contents and source code to this article
share this article :
post comment
 
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.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Team Foundation Server Hosting
Become a Sponsor