About This Code
Brief Description:
NumberRange class
Contributor:
Johan Känngård
Notes Version:
R4.x, R5.x, R6.x
Last Modified:
17 Dec 2002
OpenNTF Disclaimer
All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.
Code / Description
To encapsulate a range of numbers.
Public Class NumberRange
lo As Variant
hi As Variant
Public Sub new(lo As Variant, hi As Variant)
If Not Isscalar(lo) And Isscalar(hi) Then Error 2000, "Not scalars"
If Not Typename(lo) = Typename(hi) Then Error 2000, "Not same type"
If Not Isnumeric(lo) And Not Isnumeric(hi) Then Error 2000, "Not numerics"
Me.lo = lo
Me.hi = hi
End Sub
Public Function getMinimum() As Variant
' Returns the minimum number in this range.
getMinimum = lo
End Function
Public Function getMaximum() As Variant
' Returns the maximum number in this range.
getMaximum = hi
End Function
Public Function includesNumber(o As Variant) As Variant
' Tests whether the specified number occurs within this range.
includesNumber = (o => lo And o =< hi)
End Function
Public Function includesRange(r As NumberRange) As Variant
' Tests whether the specified range occurs entirely within this range.
includesRange = includesNumber(r.getMinimum()) _
And includesNumber(r.getMaximum())
End Function
Public Function overlaps(r As NumberRange) As Variant
' Tests whether the specified range overlaps with this range.
overlaps = includesNumber(r.getMinimum()) Or includesNumber(r.getMaximum())
End Function
Public Function equals(o As Variant) As Variant
' Indicates whether some other object is "equal" to this one.
equals = False
If Not Typename(o) = Typename(Me) Then Exit Function
equals = ((o.getMinimum() = lo) And (o.getMaximum() = hi))
End Function
Public Function toString() As String
' Returns the string representation of this range.
toString = getMinimum() & " to " & getMaximum()
End Function
End Class
Usage / Example
Dim range As New NumberRange(0, 10)
Print range.getMinimum() ' Gets 0
Print range.getMaximum() ' Gets 10
Print range.includesNumber(2) ' Gets True
Print range.includesNumber(-1) ' Gets False
Dim range2 As New NumberRange(11, 15)
Print range.includesRange(range2) ' Gets Fals
Dim range3 As New NumberRange(2, 7)
Print range.includesRange(range3) ' Gets True
Print range.overlaps(range2) ' Gets False
Print range.overlaps(range3) ' Gets True
Dim range4 As New NumberRange(9, 20)
Print range.overlaps(range4) ' Gets True
Print range.equals(range2) ' Gets False
Dim range5 As New NumberRange(0, 10)
Print range.equals(range5) ' Gets True
Print range.toString() ' Gets "0 to 10"