In VB.NET, I have a class that implements a range of numbers, call it NumericRange(Of T). Internally, NumericRange stores T as a Nullable, T?. I have another class that wraps this class as NumericRange(Of UInt16). Call this class MyNumRange (I’m being simplistic here).
So In MyNumRange, I have a few constructors defined:
Public Sub New(ByVal num1 As UInt16?, ByVal num2 As UInt16?)
' Code
End Sub
Public Sub New(ByVal num As UInt16, ByVal flag As Boolean)
' Code
End Sub
Public Sub New(ByVal num As UInt16)
' Code
End Sub
In some code outside of MyNumRange, I try to instantiate an open-ended range. That is, a range value where one of the operands is missing to represent a greater-than-or-equal to scenario. I.e., Calling New MyNumRange(32000, Nothing) should equate (after calling MyNumRange‘s overridden ToString method) to 32000 ~ (note the trailing space, and assume ~ is the delimiter).
Except, calling New MyNumRange(32000, Nothing) doesn’t jump to the constructor with a signature of New(UInt16?, UInt16?), but to New(UInt16?, Boolean) instead. This causes NumericRange to process the number 32000 as a single, specific value, not the open-ended range.
My question is, how can I use the constructors as I have them defined above in such a way that I can pass a Nothing value to the second argument of the New(UInt16?, UInt16?) constructor, it gets translated into Nothing, and num2.HasValue, if called from within the constructor, would report False?
Do I need to rethink how I have my constructors set up?
The default constructor of
Nullable<T>can be utilized. When called asnew Nullable<UInt16>(), it will act as a nullable with no value. In VB terms, you should be able to doNew Nullable(of UInt16)().