1

I have the following line of code:

CType(IIf(CBool(product.IsDiscontinued Is Nothing Or product.IsDiscontinued = True), False, True), Boolean?)

What does the Boolean? mean at the end. I have seen it used on other data types as well.

xanatos
  • 109,618
  • 12
  • 197
  • 280
user489041
  • 27,916
  • 55
  • 135
  • 204
  • (as a note, the [tag:nullable] was added by me. It was already used by more than 400 questions. The OP clearly didn't know it was nullable. I've removed the [tag:vb] tag because the code is clearly vb.net) – xanatos Sep 15 '11 at 17:46
  • @xanatos You should put comments like that in the **Edit Summary** field. That's what it's there for! – dlev Sep 15 '11 at 17:49
  • @dlev But I forgot to do it :-(, and I had to repair before someone pointed out "hey but he did know about nullable" – xanatos Sep 15 '11 at 17:51
  • @xanatos Fair enough. Wasn't sure if you knew about the field or not. :) – dlev Sep 15 '11 at 17:52

3 Answers3

5

That's a Nullable(Of Boolean).

It allows value types to be Nothing.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • +1, Nitpick: Value types can already be `Nothing` in VB.Net. `Nothing` is treated as the equivalent of `default(T)`. – JaredPar Sep 15 '11 at 17:43
  • Yes; I actually knew that. However, `null` isn't VB, so there's not much else to say. – SLaks Sep 15 '11 at 17:44
5

The ? at the end is a shortcut for Nullable<T>, in this case Nullable<Boolean>.

Using Nullable allows you to store null inside of a value type that you wouldn't otherwise be able to.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
2

Nullable... It's a nullable boolean...

As a quick aside, in the back end, these can create boxing/unboxing fun if you're not careful...

Here's a nice article explaining it (though it's written for c#)

http://msmvps.com/blogs/luisabreu/archive/2008/04/26/c-and-nullable-value-types.aspx

Rikon
  • 2,688
  • 3
  • 22
  • 32
  • Yup, you're right... I caught myself and corrected... sorry for the misprint! – Rikon Sep 15 '11 at 17:45
  • `these can create boxing/unboxing fun if you're not careful` How should they create boxing/unboxing more easily than their base type? – xanatos Sep 15 '11 at 17:50
  • I was referring to the fact that the clr has specific code designed for boxing/unboxing nullables... Not that they were necessarily easier to box... This is from Richter: "Specifcally, when the CLR is boxing a Nullable instance, it checks to see if it is null, and if so, the CLR doesn’t actually box anything, and null is returned If the nullable instance is not null, the CLR takes the value out of the nullable instance and boxes it In other words, a Nullable with a value of 5 is boxed into a boxed-Int32 with a value of 5"... I was merely making an aside that boxing was different – Rikon Sep 15 '11 at 19:04
  • Ah ok :-) Now it's clearer! :-) :-) But then why "If you are not careful"? It seems more a warning: if you do something wrong, you'll cause boxing. – xanatos Sep 15 '11 at 19:09