Skip to content

Commit

Permalink
Nothing is the default, don't bother specifying since we can't get …
Browse files Browse the repository at this point in the history
…it right for structs - fixes #1056
  • Loading branch information
GrahamTheCoder committed Dec 10, 2023
1 parent 8d57a1c commit 781de29
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 4 additions & 2 deletions CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1408,10 +1408,12 @@ public override async Task<CSharpSyntaxNode> VisitParameter(VBSyntax.ParameterSy
var arg = CommonConversions.CreateAttributeArgumentList(SyntaxFactory.AttributeArgument(defaultExpression));
_extraUsingDirectives.Add("System.Runtime.InteropServices");
_extraUsingDirectives.Add("System.Runtime.CompilerServices");
var optionalAttributes = new[] {
var optionalAttributes = new List<AttributeSyntax> {
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("Optional")),
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("DefaultParameterValue"), arg)
};
if (!node.Default.Value.IsKind(VBasic.SyntaxKind.NothingLiteralExpression)) {
optionalAttributes.Add(SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("DefaultParameterValue"), arg));
}
attributes.Insert(0,
SyntaxFactory.AttributeList(SyntaxFactory.SeparatedList(optionalAttributes)));
} else {
Expand Down
20 changes: 16 additions & 4 deletions Tests/CSharp/ExpressionTests/ByRefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ public static bool CallingFunc()
public async Task RefAfterOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Sub S(Optional a As Integer = 0, Optional ByRef b As Integer = 0)
S()
End Sub
Sub S(Optional a As Integer = 0, Optional ByRef b As Integer = 0)
S()
End Sub
", @"
public void S([Optional, DefaultParameterValue(0)] int a, [Optional, DefaultParameterValue(0)] ref int b)
{
Expand All @@ -358,6 +358,18 @@ public void S([Optional, DefaultParameterValue(0)] int a, [Optional, DefaultPara
}");
}

[Fact]
public async Task DateRefAfterOptionalArgumentAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"
Sub S(Optional ByRef dt As Date = Nothing)
End Sub
", @"
public void S([Optional] ref DateTime dt)
{
}");
}

[Fact]
public async Task OutOptionalArgumentAsync()
{
Expand All @@ -378,7 +390,7 @@ End Sub
public partial class OptionalOutIssue882
{
private void TestSub(out int a, [Optional, DefaultParameterValue(default(int))] out int b)
private void TestSub(out int a, [Optional] out int b)
{
a = 42;
b = 23;
Expand Down

0 comments on commit 781de29

Please sign in to comment.