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 = _
Private o_WeekdayEnumerator As _
WeekdayEnumerator = New DayEnumerator
private DayEnumerator o_DayEnumerator =
private WeekdayEnumerator o_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 _
If Not WeekdaysOnly Then
Return CType(o_DayEnumerator, IEnumerator)
Return CType( _
public IEnumerator GetEnumerator()
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.