Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

'For-Each' of My Own : Page 3

The .NET Framework provides many new collection classes that you can iterate (for-each) through. Get the basics on what lies beneath the surface of all these new collection types.


advertisement
Something Extra
Here's a cool feature you can put in your FiscalMonth class, and this feature certainly justifies separating it from the DayEnumerator class; you can create another enumerator class called WeekdayEnumerator. This class would be very similar to the first enumerator with one difference. You can add the functionality to the MoveNext method that checks the dt_Date value after the i_Pos counter was incremented. If dt_Date's value is a Saturday or Sunday, you can increment the i_Pos counter again, either one or two more times.

Now you have two enumerators that your FiscalMonth class can use. However, you can only have one implementation of the GetEnumerator method, so you need a variable to note which enumerator to use. First you would need to add a declaration of your new enumerator along with the old one.

In VB .NET:

Private o_DayEnumerator As DayEnumerator = _ New DayEnumerator Private o_WeekdayEnumerator As _ WeekdayEnumerator = New DayEnumerator

In C#:

private DayEnumerator o_DayEnumerator = new DayEnumerator(); private WeekdayEnumerator o_WeekdayEnumerator = new WeekdayEnumerator();

Then you need a variable to determine which enumerator to use. Ideally you should set up an Enum but for simplicity I will just use a Boolean called WeekdayOnly, with a value of False to indicate the use of the DayEnumerator and a value of True to indicate the use of the WeekdayEnumerator.

Finally, you need to modify the GetEnumerator class to determine which enumerator to use based on the value of this variable.

In VB .NET:



Public Function GetEnumerator() As _ System.Collections.IEnumerator Implements _ System.Collections.IEnumerable.GetEnumerator If Not WeekdaysOnly Then Return CType(o_DayEnumerator, IEnumerator) Else Return CType( _ o_WeekdayEnumerator, IEnumerator) End If End Function

In C#:

public IEnumerator GetEnumerator() { if(!WeekdaysOnly) return (IEnumerator)o_DayEnumerator; else return (IEnumerator)o_WeekdayEnumerator; }

When iterating you would just set this property from your client code before executing a For Each statement on your FiscalMonth class.

Learning techniques such as this gives you more insight into the inner workings of the .NET Framework; workings that were hidden from us in Visual Basic 6.0 but are exposed and extendible in .NET. This is crucial if you want to take full advantage of all of the .NET Framework's great features and is a concept I plan to keep in mind for any future articles.

The code accompanying this article contains the full code used here, plus the all-in-one FiscalMonth class with the built-in enumerator, along with the complete WeekdayEnumerator class.



Miguel A. Castro is President of InfoTek Consulting Group, Inc., a professional consulting firm that specializes in architecting, designing, and developing solutions using Microsoft .NET technologies. His VB background goes all the way back to 1.0. Miguel's focus for the last couple of years has been the .NET Framework and languages; he is fluent in both VB .NET and C#. His programming experience goes back 20 years when he started on TRS-80s, Apple IIs, and Atari 800 computers. Miguel lives in Lincoln Park, New Jersey with his wife Elena and his daughter Victoria.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date