Skip to content

Commit 1b5ac8e

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. Co-authored-by: GrahamTheCoder <2490482+GrahamTheCoder@users.noreply.github.com>
1 parent 18f8528 commit 1b5ac8e

1 file changed

Lines changed: 1 addition & 1 deletion

File tree

CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ 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 (node.Expression is VBSyntax.MemberAccessExpressionSyntax || node.Expression is VBSyntax.IdentifierNameSyntax identifier && _semanticModel.GetSymbolInfo(identifier).Symbol.IsKind(SymbolKind.Property)) {
323+
if (_semanticModel.GetSymbolInfo(node.Expression).Symbol?.IsKind(SymbolKind.Property) == true) {
324324
var (tempVarDecl, tempVar) = CreateLocalVariableWithUniqueName(node.Expression, "arg" + csTargetArrayExpression.ToString().Split('.').Last(), csTargetArrayExpression);
325325
var resizeArgs = new[] { (ExpressionSyntax)tempVar, convertedBounds.Single() }.CreateCsArgList(SyntaxKind.RefKeyword);
326326
var resizeCall = SyntaxFactory.InvocationExpression(ValidSyntaxFactory.MemberAccess(nameof(Array), nameof(Array.Resize)), resizeArgs);

0 commit comments

Comments
 (0)