Friday, May 25, 2012

How to work with C# null Boolean Type ?

Say you have a Model like below

Model

public bool? IsAllowInvoice { get; set; }

At this point when your using above model inside a MVC Controller or View you must adhere below mentioned checking to Avoid Awful Results.

Good Practices

if (IsAllowInvoice.HasValue && !IsAllowInvoice.Value)  = true // Then IsAllowInvoice is false


if (IsAllowInvoice.HasValue && IsAllowInvoice.Value) = true //Then IsAllowInvoice is true


if (!IsAllowInvoice.HasValue)  = true //Then IsAllowInvoice is null


Important Point :

When you use above method You always need to check HasValue before
check the Value (like above).Otherwise It will give InvalidOperationException.

P.S. 1
In detail Answer for Comment Posted by Anonymous October 11, 2012 5:44 PM Developer.

I have mentioned above type of solution,Because which we can use if statements
without giving true / false or null as equal parameters.
Please look at below mentioned Scenario.

Scenario :

I want to hide Invoice radio button when IsAllowInvoice is false.
In other words Show Invoice radio button when IsAllowInvoice is true.


<%  if (Model.IsAllowInvoice.HasValue && Model.IsAllowInvoice.Value)  { %>
           <li id="invoiceService">
                   <label for="invoice">  Invoice </label>
                    <input type="radio" id="invoice" name="type" value="invoice" />
         </li>
 <%%>


P.S. 2   Mukesh Selvaraj gave a very good simple solution for null boolean handling.
            That is we can use below mentioned syntax for that.Thanks for Mukesh
           (for more details  please see comment section).


<%  if (Convert.ToBoolean(IsAllowInvoice))  { %>
           <li id="invoiceService">
                   <label for="invoice">  Invoice </label>
                    <input type="radio" id="invoice" name="type" value="invoice" />
         </li>
 <% } %>


Happy Coding.

May Be Use Full To You :
How to Improve Performance of Entity Framework Query (over 400 times)?

9 comments:

  1. Totally wrong. You can just do a straight operator comparison like so:
    if (IsAllowInvoice == true)
    or
    if (IsAllowInvoice == false)
    or
    if (IsAllowance == null)

    Please read up on the fundamentals of nullable types and avoid giving unsound advice.

    ReplyDelete
    Replies
    1. Hi,

      Your explanation is true.But my idea was different.Please see the P.S. Section of above post for more details.

      Thanks for Your comment.

      Delete
  2. Totally with the post above.

    ReplyDelete
    Replies
    1. Hi,

      I couldn't get what your trying to say.If you can mentioned it clear then I can give my idea.

      Any way Thanks for Your comment.

      Delete
  3. what is the difference between
    IsAllowInvoice.HasValue and IsAllowInvoice != null

    Why cant we use string.isnullorempty(IsAllowInvoice)
    or create extenal conversion method for IsAllowInvoice.Tobool()

    Thanks in advance

    ReplyDelete
    Replies
    1. -- Edit --
      string.isnullorempty(IsAllowInvoice.tostring())

      Delete
    2. Hi Mukesh,

      Q 1 : what is the difference between ..... ?

      A 1 : Its perfectly OK to use IsAllowInvoice != null on behalf of IsAllowInvoice.HasValue. But when we consider code readability and clearness its more with IsAllowInvoice.HasValue operation (I feel).

      Q 2 : Why..string.IsNullOrEmpty(IsAllowInvoice.ToString())?

      A 2 :Yes you can use that for test null.But above scenario I have tested true/false scenario also.So then you have to use IsAllowInvoice.Value as a 2nd operator.

      Q 3 :create..conversion method for IsAllowInvoice.Tobool()?

      A 3 :Your syntax is wrong but yes your suggestion is grate.Its work with all scenarios without any problem(For null/true/false scenarios).Correct syntax is Convert.ToBoolean(IsAllowInvoice)
      I have added your solution in above post in P.S. 2 section also.

      Thanks for Your comment.

      Delete
    3. Hi,

      I agree with you. Nice approach.

      Thanks

      Delete
  4. you can use the isAllowIInvoice.GetValueOrDefault(false)

    ReplyDelete

Thanks for your Feedback.