Skip to content

Commit dfe5298

Browse files
Fix CS0206 when converting ReDim Preserve on a property
When `ReDim Preserve` is used on a property in VB.NET, the C# converter was generating `Array.Resize(ref Property, ...)` which causes CS0206 because properties cannot be passed as `ref` or `out`. This change updates `MethodBodyExecutableStatementVisitor.cs` to detect when the target of `ReDim Preserve` is a property or member access. In such cases, it generates a temporary variable, assigns the property value to it, resizes the temporary variable using `ref`, and then assigns the temporary variable back to the property. Example: VB.NET: ```vb Public Property NumArray1 As Integer() ... ReDim Preserve NumArray1(4) ``` Converted C#: ```csharp var argNumArray1 = NumArray1; Array.Resize(ref argNumArray1, 5); NumArray1 = argNumArray1; ``` This ensures valid C# code is generated. Code generation was also refactored to remove duplicate syntax logic for cleaner compilation. Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
1 parent 1b5ac8e commit dfe5298

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -320,16 +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-
if (_semanticModel.GetSymbolInfo(node.Expression).Symbol?.IsKind(SymbolKind.Property) == true) {
324-
var (tempVarDecl, tempVar) = CreateLocalVariableWithUniqueName(node.Expression, "arg" + csTargetArrayExpression.ToString().Split('.').Last(), csTargetArrayExpression);
325-
var resizeArgs = new[] { (ExpressionSyntax)tempVar, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
326-
var resizeCall = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), resizeArgs);
327-
var assignment = SyntaxFactory.AssignmentExpression(SyntaxKind.SimpleAssignmentExpression, csTargetArrayExpression, tempVar);
328-
return SyntaxFactory.List(new StatementSyntax[] { tempVarDecl, SyntaxFactory.ExpressionStatement(resizeCall), SyntaxFactory.ExpressionStatement(assignment) });
329-
}
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;
330326

331-
var argumentList = new[] { csTargetArrayExpression, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
327+
var argumentList = new[] { resizeArg, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
332328
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+
333335
return SingleStatement(arrayResize);
334336
}
335337
var newArrayAssignment = CreateNewArrayAssignment(node.Expression, csTargetArrayExpression, convertedBounds);

0 commit comments

Comments
 (0)