Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Convert Date literals #159

Closed
kanonrai opened this issue Jul 25, 2018 · 1 comment
Closed

VB -> C#: Convert Date literals #159

kanonrai opened this issue Jul 25, 2018 · 1 comment
Assignees
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@kanonrai
Copy link

kanonrai commented Jul 25, 2018

Input code

Public Function foo(Option ByVal date As Date = #1/1/1900#)

Erroneous output

Error message:
System.ArgumentOutOfRangeException: Exception of type 'System.ArgumentOutOfRangeException' was thrown.
Parameter name: value
Actual value was 1/1/1900 12:00:00 AM.
   at ICSharpCode.CodeConverter.CSharp.CommonConversions.GetLiteralExpression(Object value, String valueText)
   at ICSharpCode.CodeConverter.CSharp.VisualBasicConverter.NodesVisitor.VisitLiteralExpression(LiteralExpressionSyntax node)
   at Microsoft.CodeAnalysis.VisualBasic.VisualBasicSyntaxVisitor`1.Visit(SyntaxNode node)

Details

Product in use: vs extension

@kanonrai kanonrai changed the title VB -> C#: cannot convert CDate('1900/01/01') VB -> C#: cannot convert Date literals Jul 25, 2018
@GrahamTheCoder GrahamTheCoder added the VB -> C# Specific to VB -> C# conversion label Jul 26, 2018
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Jul 26, 2018

Thanks for the bug report with minimal example. In most cases it'll be possible to do something like DateTime.Parse("1/1/1900").

In the case of const declarations, they'll need to be made non-const to allow this.

In the case you've shown of optional parameters I'll aim to do something like this:
Jon Skeet seems to say it worked for him

public int foo([Optional, DateTimeConstant(0L)] DateTime date)

where 0L would be the ticks representation of the default value given.
This will need to deal with inlining a const field as well as just constant literals.

If it doesn't work I'll have to use something more like:

public int foo(DateTime? optionalDate = null)
{
    var date = optionalDate ?? DateTime.Parse("1/1/1900");
    // Rest of method body
}

Attributes taking DateTime parameters don't seem to be supported in .NET fortunately

@GrahamTheCoder GrahamTheCoder changed the title VB -> C#: cannot convert Date literals VB -> C#: Convert Date literals Aug 17, 2018
@GrahamTheCoder GrahamTheCoder self-assigned this Nov 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants