question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

VB -> C#: Problem converting long if statement with nullable integers

See original GitHub issue

VB.Net input code

'Dim newDays As Integer?
'Dim oldDays As Integer?

If (newDays.HasValue AndAlso Not oldDays.HasValue) _
                OrElse (newDays.HasValue AndAlso oldDays.HasValue AndAlso newDays <> oldDays) _
                OrElse (Not newDays.HasValue AndAlso oldDays.HasValue) Then

'Some code
End If

Erroneous output

if ((bool)((newDays.HasValue && !oldDays.HasValue ? true : newDays.HasValue && oldDays.HasValue ? newDays is var arg1 && oldDays is { } arg2 && arg1.HasValue ? arg1 != arg2 : null : false).GetValueOrDefault() || !newDays.HasValue && oldDays.HasValue))
// Some code


Expected output

if ((newDays.HasValue && !oldDays.HasValue) || (newDays.HasValue && oldDays.HasValue && newDays != oldDays) || (!newDays.HasValue && oldDays.HasValue))

Details

  • Product in use: e.g. both
  • Version in use: e.g. 9.1.2
  • Did you see it working in a previous version, which? No
  • Any other relevant information to the issue, or your interest in contributing a fix.

Issue Analytics

  • State:closed
  • Created 8 months ago
  • Comments:12 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
GrahamTheCodercommented, Feb 1, 2023

Should be available now. I know some cases will still be ugly so if you see other nullable related issues in the new version, feel free to reopen this with as many examples as you can find. Thanks!

1reaction
GrahamTheCodercommented, Jan 30, 2023

Thanks for the thoughts on this. After putting together #984 I have even more appreciation for your previous work on it (and the tests that went with it) so thanks again for that too. I made a start on points 1 and 3, and believe I have dealt with the Conversions.ToInteger issue. That’s about all the time I have to work on this for now, so feel free to pick up where I left off if you’re interested. I’ll close this issue since I believe I’ve covered the examples given, but just reopen it or create a new issue if there are more examples worth fixing

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - if condition with nullable
It's because a nullable bool cannot be implicitly converted to a bool. It's the same reason why you can do this : int...
Read more >
c# - Convert a boxed integer to an nullable ...
The ChangeType(Object, Type) method can convert a nullable type to another type. However, it cannot convert another type to a value of a ......
Read more >
Resolve nullable warnings
Several compiler warnings indicate code that isn't null-safe. Learn how to address those warnings by making your code more resilient.
Read more >
Null-conditional Operators - Visual Basic
The null-conditional operators are short-circuiting. If one operation in a chain of conditional member access and index operations returns ...
Read more >
Solved: Expression.Error: We cannot convert the value null...
Everything were fine until yesterday when I started to get error Expression.Error: We cannot convert the value null to type Number from several...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found