-
Jan 12th, 2021, 12:23 PM
#1
Thread Starter
PowerPoster
[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.
-
Jan 12th, 2021, 12:35 PM
#2
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
-
Jan 12th, 2021, 01:34 PM
#3
Thread Starter
PowerPoster
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.
-
Jan 20th, 2021, 11:21 PM
#4
Re: IDE0031 "Null check can be simplified", but can it?
Originally Posted by MMock
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|