19

Possible Duplicate:
Which is preferred: Nullable<>.HasValue or Nullable<> == null?

I know questions like this have been asked many times. But I never found an answer to how to check if a nullable bool is null or not. Here is an answer I have to this:

bool? nullableBool;
if (nullableBool == true){

}else if (nullableBool == false){

}else{

}

But I was wondering if there is a better and more straight to the point way in order to minimize useless codes? Thanks.

Community
  • 1
  • 1
Amin
  • 578
  • 1
  • 6
  • 18
  • 4
    That's not a nullable bool. That's a bool that has not been assigned a value – musefan Nov 09 '12 at 08:43
  • 1
    @Paolo Editing the post like that doesn't help; it invalidates all the answers correctly telling the OP he's doing it wrong, and it still hasn't made the code correct. – Rawling Nov 09 '12 at 08:50
  • I rolled back the edit.. don't make assumptions like this, you mess up the OP's question – musefan Nov 09 '12 at 08:53
  • Sorry that was a type error. I thought I have the question mark there :P. anyways thanks. – Amin Nov 09 '12 at 09:01
  • 1
    @musefan (and Rawling): this was just a small mistake or typo, and it was perfectly clear from the rest of the question, it's not "messing up" or "making assumptions". Since we are people, and not compilers, a little tolerance would not be bad :) – Paolo Tedesco Nov 09 '12 at 09:03
  • 2
    @PaoloTedesco: it turns out it was a typo, but that doesn't excuse you for assuming it. There is nothing perfectly clear when an unknown new user to this site uses the word "nullable" that they actually even know what a nullable type really is... for all we know, they may have thought prefixing the variable name with the word "nullable" is what does the job, there are plenty of people like that in the world. Point is, you ARE making an assumption, and you are altering the question without knowing for sure what the OP meant – musefan Nov 09 '12 at 09:20
  • 1
    Yes, I know that what I said was not only perfectly reasonable, but also right ;) – Paolo Tedesco Nov 09 '12 at 09:31

4 Answers4

30
if (!nullableBool.HasValue)
{
    // null
}

You also can directly compare it with null.

tukaef
  • 9,074
  • 4
  • 29
  • 45
  • wont work with OP's code – musefan Nov 09 '12 at 08:44
  • 4
    @musefan to be fair, that's the fault of the OP's code... – Marc Gravell Nov 09 '12 at 08:45
  • @MarcGravell: Maybe, but either way this answer still doesn't work – musefan Nov 09 '12 at 08:46
  • @MarcGravell: Because the OP (before the edit) used a `bool` not a `bool?` and this question (before the edit) made no reference to using the nullable type instead, and in terms of compiling then that just doesn't work – musefan Nov 09 '12 at 09:15
  • 3
    @musefan it was abundantly clear, even before the edit, that this was their intent. The edit merely corroborates this. – Marc Gravell Nov 09 '12 at 09:29
  • 2
    @Marc, the OP is asking a simple question (simple to you and I) how to check if a nullable bool is null or not. Anybody who really knows how to use a nullable data type should know how to check for null... so if this OP doesn't know how to check, why make the assumption that he actually even knows how to declare one properly... you only think your right that it is obvious because it has subsequently turned out to the the case. if the OP had accepted my answer and said "thanks, turns out I was declaring it all wrong" then you wouldn't be so keen to state how obviously wrong your assumption was – musefan Nov 09 '12 at 09:34
11

That's not a nullable bool, it's an unassigned bool and your code won't compile. You need to use bool? or Nullable<bool>.

bool? nullableBool = null; // or = true or = false
if (nullableBool.HasValue)
{
    if (nullableBool.Value)
        // true
    else
       // false
}
else
    // null
Rawling
  • 49,248
  • 7
  • 89
  • 127
4

Firstly, the bool you have used is not nullable. To create a nullable bool you can do one fo the following:

Nullable<bool> nullableBool;

or the shorthand version:

bool? nullableBool;

either of these can then be checked to see if it has a value using the following:

if(nullableBool.HasValue)
{
   //nullableBool has been assigned a value
}

Your current approach is not recommended. If you need a nullable state then use a nullable bool. If you want to use a standard bool then be sure to assign it a value. I am surprised you don't get a compile error in Visual Studio with that code.

musefan
  • 47,875
  • 21
  • 135
  • 185
0

Try this plz:

if (!nullableBool.HasValue)
{
    // your code
}
bonCodigo
  • 14,268
  • 1
  • 48
  • 91