Skip to content

Commit cace870

Browse files
Merge pull request #22 from GrahamTheCoder/fix-redim-preserve-property-1508587937501568863
Fix ReDim Preserve on property generating invalid ref code
2 parents 355bc35 + dfe5298 commit cace870

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,18 @@ private async Task<SyntaxList<StatementSyntax>> ConvertRedimClauseAsync(VBSyntax
320320
var csTargetArrayExpression = await node.Expression.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
321321
var convertedBounds = (await CommonConversions.ConvertArrayBoundsAsync(node.ArrayBounds)).Sizes.ToList();
322322
if (preserve && convertedBounds.Count == 1) {
323-
var argumentList = new[] { csTargetArrayExpression, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
323+
bool isProperty = _semanticModel.GetSymbolInfo(node.Expression).Symbol?.IsKind(SymbolKind.Property) == true;
324+
var arrayToResize = isProperty ? CreateLocalVariableWithUniqueName(node.Expression, "arg" + csTargetArrayExpression.ToString().Split('.').Last(), csTargetArrayExpression) : default;
325+
var resizeArg = isProperty ? (ExpressionSyntax)arrayToResize.Reference : csTargetArrayExpression;
326+
327+
var argumentList = new[] { resizeArg, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
324328
var arrayResize = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), argumentList);
329+
330+
if (isProperty) {
331+
var assignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, csTargetArrayExpression, arrayToResize.Reference);
332+
return SyntaxFactory.List(new StatementSyntax[] { arrayToResize.Declaration, SyntaxFactory.ExpressionStatement(arrayResize), SyntaxFactory.ExpressionStatement(assignment) });
333+
}
334+
325335
return SingleStatement(arrayResize);
326336
}
327337
var newArrayAssignment = CreateNewArrayAssignment(node.Expression, csTargetArrayExpression, convertedBounds);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Threading.Tasks;
2+
using ICSharpCode.CodeConverter.Tests.TestRunners;
3+
using Xunit;
4+
5+
namespace ICSharpCode.CodeConverter.Tests.CSharp.StatementTests;
6+
7+
public class RedimPreserveTests : ConverterTestBase
8+
{
9+
[Fact]
10+
public async Task RedimPreserveOnPropertyAsync()
11+
{
12+
await TestConversionVisualBasicToCSharpAsync(
13+
@"Public Class TestClass
14+
Public Property NumArray1 As Integer()
15+
16+
Public Sub New()
17+
ReDim Preserve NumArray1(4)
18+
End Sub
19+
End Class", @"using System;
20+
21+
public partial class TestClass
22+
{
23+
public int[] NumArray1 { get; set; }
24+
25+
public TestClass()
26+
{
27+
var argNumArray1 = NumArray1;
28+
Array.Resize(ref argNumArray1, 5);
29+
NumArray1 = argNumArray1;
30+
}
31+
}");
32+
}
33+
}

0 commit comments

Comments
 (0)