[SerializableAttribute]
public class HolidayCollection : Collection<Holiday>
<SerializableAttribute>
Public Class HolidayCollection
Inherits Collection(Of Holiday)
[SerializableAttribute]
public ref class HolidayCollection : public Collection<Holiday^>
[<SerializableAttribute>]
type HolidayCollection =
class
inherit Collection<Holiday>
end
DateTime testDate;
int yearFrom = 1998, yearTo = 2006;
// Create a set of fixed and floating holidays
HolidayCollection holidays = new HolidayCollection();
holidays.AddFixed(1, 1, true, "New Year's Day");
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 1, 0, "Martin Luther King Day");
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 2, 0, "President's Day");
holidays.AddFloating(DayOccurrence.Last, DayOfWeek.Monday, 5, 0, "Memorial Day");
holidays.AddFixed(7, 4, true, "Independence Day");
holidays.AddFloating(DayOccurrence.First, DayOfWeek.Monday, 9, 0, "Labor Day");
holidays.AddFixed(11, 11, true, "Veteran's Day");
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 0, "Thanksgiving Day");
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 1, "Day After Thanksgiving");
holidays.AddFixed(12, 25, true, "Christmas Day");
// Display the holidays added to the list
Console.WriteLine("Holidays on file. Is Holiday should be true for all.");
foreach(Holiday hol in holidays)
Console.WriteLine("Holiday Date: {0:d} Is Holiday: {1} Description: {2}",
hol.ToDateTime(yearFrom), holidays.IsHoliday(hol.ToDateTime(yearFrom)),
hol.Description);
// Display holidays found in each year specified using the IsHoliday method
Console.WriteLine("Looking for holidays using the IsHoliday method");
testDate = new DateTime(yearFrom, 1, 1);
while(testDate.Year <= yearTo)
{
if(holidays.IsHoliday(testDate))
Console.WriteLine("Found holiday: {0:d}", testDate);
testDate = testDate.AddDays(1);
}
// One more time, but use a hash set using the dates returned by the HolidaysBetween()
// method. For bulk comparisons, this is faster than the above procedure using the
// IsHoliday method.
Console.WriteLine("Looking for holidays using HolidaysBetween");
var holidayDates = new HashSet<DateTime>(holidays.HolidaysBetween(yearFrom, yearTo));
if(holidayDates.Count != 0)
{
testDate = new DateTime(yearFrom, 1, 1);
while(testDate.Year <= yearTo)
{
if(holidayDates.Contains(testDate))
Console.WriteLine("Found holiday: {0:d} {1}", testDate,
holidays.HolidayDescription(testDate));
testDate = testDate.AddDays(1);
}
}
Dim testDate As DateTime
Dim holidayDates As HashSet(Of DateTime)
Dim yearFrom As Integer = 1998
Dim yearTo As Integer = 2006
Dim hol As Holiday
' Create a set of fixed and floating holidays
Dim holidays As New HolidayCollection()
holidays.AddFixed(1, 1, True, "New Year's Day")
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 1, 0, "Martin Luther King Day")
holidays.AddFloating(DayOccurrence.Third, DayOfWeek.Monday, 2, 0, "President's Day")
holidays.AddFloating(DayOccurrence.Last, DayOfWeek.Monday, 5, 0, "Memorial Day")
holidays.AddFixed(7, 4, True, "Independence Day")
holidays.AddFloating(DayOccurrence.First, DayOfWeek.Monday, 9, 0, "Labor Day")
holidays.AddFixed(11, 11, True, "Veteran's Day")
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 0, "Thanksgiving Day")
holidays.AddFloating(DayOccurrence.Fourth, DayOfWeek.Thursday, 11, 1, "Day After Thanksgiving")
holidays.AddFixed(12, 25, True, "Christmas Day")
' Display the holidays added to the list
Console.WriteLine("Holidays on file. Is Holiday should be true for all.")
For Each hol In holidays
Console.WriteLine("Holiday Date: {0:d} Is Holiday: {1} Description: {2}",
hol.ToDateTime(yearFrom), holidays.IsHoliday(hol.ToDateTime(yearFrom)),
hol.Description)
Next
' Display holidays found in each year specified using the IsHoliday method
Console.WriteLine("Looking for holidays using the IsHoliday method")
testDate = New DateTime(yearFrom, 1, 1)
Do While testDate.Year <= yearTo
If holidays.IsHoliday(testDate) = True Then
Console.WriteLine("Found holiday: {0:d}", testDate)
End If
testDate = testDate.AddDays(1)
Loop
' One more time, but use a hash set using the dates returned by the HolidaysBetween()
' method. For bulk comparisons, this is faster than the above procedure using the
' IsHoliday method.
Console.WriteLine("Looking for holidays using HolidaysBetween")
var holidayDates = new HashSet(Of DateTime)(holidays.HolidaysBetween(yearFrom, yearTo));
If holidayDates.Count != 0 Then
testDate = New DateTime(yearFrom, 1, 1)
Do While testDate.Year <= yearTo
If holidayDates.Contains(testDate) = True Then
Console.WriteLine("Found holiday: {0:d} {1}", testDate,
holidays.HolidayDescription(testDate))
End If
testDate = testDate.AddDays(1)
Loop
End If
No code example is currently available or this language may not be supported.
No code example is currently available or this language may not be supported.
HolidayCollection | Default constructor |
HolidayCollection(IEnumerableHoliday) | Construct the collection from an enumerable list of Holiday objects |
Count | Gets the number of elements actually contained in the CollectionT. (Inherited from CollectionHoliday) |
Item | Gets or sets the element at the specified index. (Inherited from CollectionHoliday) |
Items | Gets a IListT wrapper around the CollectionT. (Inherited from CollectionHoliday) |
Add | Adds an object to the end of the CollectionT. (Inherited from CollectionHoliday) |
AddFixed | Add a new holiday object to the collection that occurs on a fixed date |
AddFloating | Add a new holiday object to the collection that occurs on a floating date |
AddRange | Add a range of Holiday instances from an enumerable list |
AddStandardHolidays | This adds a standard set of United States holidays to the collection |
Clear | Removes all elements from the CollectionT. (Inherited from CollectionHoliday) |
ClearItems | Removes all elements from the CollectionT. (Inherited from CollectionHoliday) |
Contains | Determines whether an element is in the CollectionT. (Inherited from CollectionHoliday) |
CopyTo | Copies the entire CollectionT to a compatible one-dimensional Array, starting at the specified index of the target array. (Inherited from CollectionHoliday) |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object) |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object) |
GetEnumerator | Returns an enumerator that iterates through the CollectionT. (Inherited from CollectionHoliday) |
GetHashCode | Serves as the default hash function. (Inherited from Object) |
GetType | Gets the Type of the current instance. (Inherited from Object) |
HolidayDescription | This method returns a description if the specified date falls on a holiday or an empty string if it does not. |
HolidaysBetween | This method returns an enumerable list of holidays between the given years |
IndexOf | Searches for the specified object and returns the zero-based index of the first occurrence within the entire CollectionT. (Inherited from CollectionHoliday) |
Insert | Inserts an element into the CollectionT at the specified index. (Inherited from CollectionHoliday) |
InsertItem | Inserts an element into the CollectionT at the specified index. (Inherited from CollectionHoliday) |
IsHoliday | This method returns true if the specified date falls on a holiday or false if it does not |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object) |
Remove | Removes the first occurrence of a specific object from the CollectionT. (Inherited from CollectionHoliday) |
RemoveAt | Removes the element at the specified index of the CollectionT. (Inherited from CollectionHoliday) |
RemoveItem | Removes the element at the specified index of the CollectionT. (Inherited from CollectionHoliday) |
SetItem | Replaces the element at the specified index. (Inherited from CollectionHoliday) |
ToString | Returns a string that represents the current object. (Inherited from Object) |