Results 1 to 4 of 4

Thread: [RESOLVED] IDE0031 "Null check can be simplified", but can it?

  1. #1

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Resolved [RESOLVED] IDE0031 "Null check can be simplified", but can it?

    Probably it can, but I am not sure how, for reason stated in my comment!
    Is there a better way to write this?

    Code:
                    
    int rc = taCustomerSystemData.Update(txtCustNo.Text,
    // 01/12/21 - IDE0029 "Null check can be simplified" - but I am not sure how.  
    //  If it is null then I want null but if it's not null I want to covert it to a string.
    dtLastSDReview.EditValue == null ? null : dtLastSDReview.EditValue.ToString(),
    ...
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  2. #2
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: IDE0031 "Null check can be simplified", but can it?

    Depending on your version you can do

    dtLastSDReview.EditValue?.ToString()

    It will either return null or the tostring of EditValue

  3. #3

    Thread Starter
    PowerPoster MMock's Avatar
    Join Date
    Apr 2007
    Location
    Driving a 2018 Mustang GT down Route 8
    Posts
    4,475

    Re: IDE0031 "Null check can be simplified", but can it?

    Thank you; worked beautifully!
    There are 10 kinds of people in this world. Those who understand binary, and those who don't.

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: IDE0031 "Null check can be simplified", but can it?

    Quote Originally Posted by MMock View Post
    Thank you; worked beautifully!
    They've added some useful shortcuts like this. Another one we use constantly is ??. If the value to the left is null, it returns the one to the right.
    example:

    object?.property ?? 5

    Also the single ? is a shortcutting null check.
    If you have something like this:
    my?.property?.that?.is?.null
    the compiler will not evaluate anything after it hits the first null property from the left.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width