.NET Tips and Tricks

Blog archive

Accepting Nullable Data

Sometimes you get null or Nothing passed to parameters for methods in your application. Sometimes that's not OK, but sometimes it is -- especially if you're accepting data from a database. If you're accepting a value parameter (like an integer), you may have been declaring your parameters as object so that you accept nulls. There is a better solution: you can add a question mark to your type declaration to indicate that it's OK to pass a null/Nothing value to that parameter:

Sub AcceptingParms(parmNullsOK As Integer?)

  If parmNullsOK IsNot Nothing Then

  Else

  End If

End Sub

You're not limited to using the question mark in parameters or with the Integer/int datatype. You can use it on any value type where you're willing to accept a null/Nothing value. Do be aware, though, that these nullable types are a different datatype from their non-nullable cousins (i.e. int? is not int and Integer? is not Integer). So if you want to use a nullable value with a non-nullable value, you'll have to do a conversion:

Dim res As Integer
Dim num? As Integer = 2
res = Integer.Parse(num) + Integer.Parse(num)

What you're actually doing when you add the question mark to the end of your datatype is creating an instance of a reference class called System.Nullable<T>. To put it another way, int? is the same as System.Nullable<int>). The question mark is just shorthand for the longer declaration.

Posted by Peter Vogel on 06/07/2011 at 1:16 PM


comments powered by Disqus

Reader Comments:

Thu, Jun 9, 2011 Peter Vogel Canada

(And I'm not at all bitter that Richard has sucked any possibility of a follow up tip from me. Not. At. All)

Thu, Jun 9, 2011 Peter Vogel Canada

Thanks for expanding on my one-liner--I just wanted to point out that an int? isn't an int. This is excellent stuff on the right/best way to combine the nullable and non-nullable types. I especially like your note around checking HasValue before using Value. If you're wondering why Richard says that I'm converting to a string, it's because the Parse method doesn't typically have many overloads and those overloads specify that the first parameter (the value being converted) is a String. I think there's an underlying message in Richard's note, also: If you're not doing a type conversion, don't use the Parse method; if you're extracting the value, use the Value property. Using Parse has other downsides besides the one that Richard points out: if anything goes wrong, Parse throws an exception and, when exceptions are thrown, everything stops for tea. If you do need to do a type conversion, TryParse is a better choice if you think that Parse will fail: TryParse doesn't throw an exception (it just returns null).

Wed, Jun 8, 2011 Richard

"res = Integer.Parse(num) + Integer.Parse(num)"

OK, so you've got an int? and you want to convert it to an int; the best way is to convert it to a string and then parse it back, right?!


Since you know your int? variable has a value, try this:

res = num.Value + num.Value


If you don't know whether it has a value, check the HasValue property.


If you want to replace a null value with a sensible default, try the GetValueOrDefault method:

res = num.GetValueOrDefault() + num.GetValueOrDefault(2)

In C#, you can also use the null-coalescing operator:

res = (num ?? 0) + (num ?? 2);


The Nullable type also lifts the operators from type T, so you could say:

res = (num + num).GetValueOrDefault()


Any of these options are much better than converting to a string and parsing the string back to the original type!

Add Your Comments Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.