Nullable Types

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Declaring a nullable type in VB.net 2.0 and determining whether or not it has a value is easy:
Code:
Dim r As Nullable(Of Long)
MsgBox(r.HasValue) 'False
r = 0
MsgBox(r.HasValue) 'True
But is there a way to set r back to the state of having no value having given it one?
 
Should have thought longer before posting. Just do:

Code:
r = Nothing

I note that Microsoft doesn't seem to use this facility itself, e.g. the SelectedIndex of a combobox is not a nullable type, so with no item selected it is -1, rather than there being no value.
 
I note that Microsoft doesn't seem to use this facility itself, e.g. the SelectedIndex of a combobox is not a nullable type, so with no item selected it is -1, rather than there being no value.
This behavior is there for two reasons that I can think of. Firstly, for backwards compatability. Secondly, nullable types are great for database purposes where values can be null (even though their .Net equivalent can't be), but for the most part, nullables are little more than a minor convinience. It takes an extra 16 or 32 bits for .Net to store the status of whether the value is null or not (the underlying boolean is stored in 16 bits and marshaled as 32), my guess is that it is slower performance-wise (access to value is done through properties/functions), and it is easy enough for one to create a boolean to indicate the validity of a value on his own. I wouldn't expect nullables to play a large role in the .Net framework classes.
 
They seem quite useful to me. If you create a Boolean to store whether or not the value is set then you've got to remember what that Boolean is every time you want to use the variable. With a nullable variable you haven't, which is much simpler.
 
rbulph said:
They seem quite useful to me. If you create a Boolean to store whether or not the value is set then you've got to remember what that Boolean is every time you want to use the variable. With a nullable variable you haven't, which is much simpler.
A simple convention makes this problem virtually nonexistant.

[Vb]
Dim _ImageIndex As Integer
Dim _ValidImageIndex As Integer

Dim PageNumber As Integer
Dim ValidPageNumber As Integer
[/CODE]

The fact is that in my experience, I never had a problem that a nullable type could have solved or even made signifigantly easier, since there are so many other solutions (which generally strike me, personally, as more intuitive).
Visual Basic:
'Enumerations
Public Enum Icon
    None = -1
    [Error] = 0
    Alert = 1
    Question = 2
    Info = 3
End Enum
 
'Special values
ImageIndex = -1 'This might not be so intuitive, but I would say that 
'it is very adequately functional and easily understood. If you don't agree
'then there is an easy solution.
Public Const NoImage As Integer = -1
ImageIndex = NoImage
 
'Booleans with names that better represent the essence of the
'"invalid" or "null" value.
Dim _IsBrowsing As Boolean
Dim CurrentPage As Integer
 
If _IsBrowsing Then
    'Is more self-explanitory than
ElseIf Not CurrentPage Is Null
End If
 
'If _IsBrowsing = False, then CurrentPage isn't null, but
'it's value simply loses relevance.

There isn't really anything wrong with Nullable types, but to me it is just another tool that I don't need to do what that I have always done without and that I won't be any better off with.
 
If you have a property showing in a propertygrid which is numerical and which need not be specified then you pretty much need a nullable type. Otherwise there's no way that the value shown can be blank rather than zero. For instance if a property represents a maximum value, specifying that this maximum value is zero means something very different from specifying that there is no maximum value. (Although I note that in the properties window in visual studio, the maximum size of a form does not work in this way - if 0,0 is specified that is understood to mean that there is no maximum size. But there could be circumstances where negative values are possible, and a maximum of zero would have real meaning).
 
In the right place Nullable Types are a really beautiful thing.

You could make the equivalent on your own, it's not hard, but requires making a generic structure and then creating static widening and narrowing operators to handle the implicit and explicit conversions.

The advantage of using the build-in Nullable generic structure is that you have the sort of 'Enum status check' that ME was showing (although only one status, really: either 'HasValue' = True or False), while allowing implicit and explicit conversions from the generic structure to-from the underlying type.

But the best thing about Nullables, versus a "roll-your-own" approach, is that it is standarized. Everyone knows what to expect and how to use it. I would only "roll-my-own" if I needed greater functionality than the .NET 2.0 Nullables provide natively.
 
Last edited:
Back
Top