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#: CType(Nothing, <Nullable>?) returns default instead of null

See original GitHub issue

VB.Net input code

Imports System

Public Class VisualBasicClass
    Dim SomeDate As String = "2022-01-01"
    Dim SomeDateDateParsed As Date? = If(String.IsNullOrEmpty(SomeDate), CType(Nothing, Date?), DateTime.Parse(SomeDate))
    Dim SomeDateDateNothing As Date? = If(String.IsNullOrEmpty(SomeDate), Nothing, DateTime.Parse(SomeDate))
End Class

Erroneous output

using System;

public partial class VisualBasicClass
{
    private string SomeDate = "2022-01-01";
    private DateTime? SomeDateDateParsed;
    private DateTime? SomeDateDateNothing;

    public VisualBasicClass()
    {
        SomeDateDateParsed = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
        SomeDateDateNothing = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
    }
}

Expected output

using System;

public partial class VisualBasicClass
{
    private string SomeDate = "2022-01-01";
    private DateTime? SomeDateDateParsed;
    private DateTime? SomeDateDateNothing;

    public VisualBasicClass()
    {
        SomeDateDateParsed = string.IsNullOrEmpty(SomeDate) ? null: DateTime.Parse(SomeDate);
        SomeDateDateNothing = string.IsNullOrEmpty(SomeDate) ? default : DateTime.Parse(SomeDate);
    }
}

Details

  • Product in use: both
  • Version in use: e.g. 9.1.3.577
  • 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. I know it’s tricky in VB with Nothing, but I think in this scenario it should be null, not default. Please check the fiddle: https://dotnetfiddle.net/PwEL1D

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
GrahamTheCodercommented, Mar 28, 2023

9.2.1 released

0reactions
cysiekscommented, Mar 27, 2023

Cool, any chance to update the command line tool? We have it in the pipeline.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my Nullable(Of Int32) = 0 after I set it to Nothing?
In VB.NET Nothing does not only mean null but also default . So you are assigning the default value of Int32 to the...
Read more >
Nothing keyword - Visual Basic
Represents the default value of any data type. For reference types, the default value is the null reference. For value types, the default...
Read more >
Nullable Value Types - Visual Basic
When you declare a variable with a nullable value type, its HasValue property has a default value of False . This means that...
Read more >
possible VB.NET compiler bug using Null-conditional ...
The reason is because the null-conditional operator isn't returning False , it's returning Nothing . VB.NET does not treat Nothing as False ...
Read more >
VB.NET Nothing, IsNothing (Null)
First example. This program assigns a String variable to Nothing. This is not an empty string. It is a String object that does...
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