DateRange
—
A range of Date type
Problem
The Problem!
Solution
The Solution!
Discussion
The Discussion!
Example
This example how to define a range of DateTime values, then checks to see if Today is within that range.
Option Strict On
Dim october2005 As New DateRange(#10/1/2005#, #10/31/2005#)
If october2005.Contains(DateTime.Today) Then
End If
Source
Define the generic Range(Of T) type.
Option Strict On
Option Explicit On
Public Structure DateRange
Private ReadOnly m_start As DateTime
Private ReadOnly m_end As DateTime
Public Sub New(ByVal start As DateTime, ByVal [end] As DateTime)
m_start = start.Date
m_end = [end].Date
End Sub
Public ReadOnly Property Start() As DateTime
Get
Return m_start
End Get
End Property
Public ReadOnly Property [End]() As DateTime
Get
Return m_end
End Get
End Property
Public ReadOnly Property IsEmpty() As Boolean
Get
Return m_start.CompareTo(Nothing) = 0 AndAlso m_end.CompareTo(Nothing) = 0
Return m_start.Equals(Nothing) AndAlso m_end.Equals(Nothing)
End Get
End Property
Public Function Contains(ByVal value As DateTime) As Boolean
Dim dateValue As DateTime = value.Date
Return m_start.CompareTo(dateValue) <= 0 AndAlso dateValue.CompareTo(m_end) <= 0
End Function
Public Function Contains(ByVal value As DateRange) As Boolean
Return Me.Contains(value.m_start) AndAlso Me.Contains(value.m_end)
End Function
Public Function Overlaps(ByVal value As DateRange) As Boolean
Return Me.Contains(value) OrElse value.Contains(m_start) OrElse value.Contains(m_end)
End Function
Public Overrides Function GetHashCode() As Integer
Return m_start.GetHashCode() Xor m_end.GetHashCode()
End Function
Public Overloads Overrides Function Equals(ByVal obj As Object) As Boolean
If TypeOf obj Is DateRange Then
Return Equals(DirectCast(obj, DateRange))
Else
Return False
End If
End Function
Public Overloads Function Equals(ByVal other As DateRange) As Boolean
Return m_start.Equals(other.m_start) AndAlso m_end.Equals(other.m_end)
End Function
End Structure
See Also