From 186c758caaea1c036805a4dced54aeefdd44e051 Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 10 Dec 2023 00:23:49 +0000 Subject: [PATCH] Exit Property now returns value assigned to return variable - fixes #1051 --- CHANGELOG.md | 1 + .../CSharp/DeclarationNodeVisitor.cs | 2 +- Tests/CSharp/StatementTests/StatementTests.cs | 57 +++++++++++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a71986262..e7b548a7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### VB -> C# +* Exit Property now returns value assigned to return variable [#1051](https://github.com/icsharpcode/CodeConverter/issues/1051) * Avoid stack overflow for very deeply nested binary expressions [#1033](https://github.com/icsharpcode/CodeConverter/issues/1033) * Omit special VB conversions within expression trees [#930](https://github.com/icsharpcode/CodeConverter/issues/930) [#316](https://github.com/icsharpcode/CodeConverter/issues/316) * Support CData [#1032](https://github.com/icsharpcode/CodeConverter/issues/1032) diff --git a/CodeConverter/CSharp/DeclarationNodeVisitor.cs b/CodeConverter/CSharp/DeclarationNodeVisitor.cs index 3f5e74845..264ad0361 100644 --- a/CodeConverter/CSharp/DeclarationNodeVisitor.cs +++ b/CodeConverter/CSharp/DeclarationNodeVisitor.cs @@ -947,7 +947,7 @@ public override async Task VisitAccessorBlock(VBSyntax.Accesso var ancestoryPropertyBlock = node.GetAncestor(); var containingPropertyStmt = ancestoryPropertyBlock?.PropertyStatement; var csReturnVariableOrNull = CommonConversions.GetRetVariableNameOrNull(node); - var convertedStatements = SyntaxFactory.Block(await ConvertMethodBodyStatementsAsync(node, node.Statements, isIterator)); + var convertedStatements = SyntaxFactory.Block(await ConvertMethodBodyStatementsAsync(node, node.Statements, isIterator, csReturnVariableOrNull)); var body = WithImplicitReturnStatements(node, convertedStatements, csReturnVariableOrNull); var attributes = await CommonConversions.ConvertAttributesAsync(node.AccessorStatement.AttributeLists); var modifiers = CommonConversions.ConvertModifiers(node, node.AccessorStatement.Modifiers, TokenContext.Local); diff --git a/Tests/CSharp/StatementTests/StatementTests.cs b/Tests/CSharp/StatementTests/StatementTests.cs index b5b426fa3..bdcbdd1fc 100644 --- a/Tests/CSharp/StatementTests/StatementTests.cs +++ b/Tests/CSharp/StatementTests/StatementTests.cs @@ -2051,6 +2051,63 @@ private IEnumerable TestMethod(int number) yield return i; yield break; } +}"); + } + + [Fact] + public async Task SetterReturnAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Public ReadOnly Property Prop() As Object + Get + Try + Prop = New Object + Exit Property + Catch ex As Exception + End Try + End Get +End Property + +Public Function Func() As Object + Try + Func = New Object + Exit Function + Catch ex As Exception + End Try +End Function", @" +internal partial class SurroundingClass +{ + public object Prop + { + get + { + object PropRet = default; + try + { + PropRet = new object(); + return PropRet; + } + catch (Exception ex) + { + } + + return PropRet; + } + } + + public object Func() + { + object FuncRet = default; + try + { + FuncRet = new object(); + return FuncRet; + } + catch (Exception ex) + { + } + + return FuncRet; + } }"); } } \ No newline at end of file