Skip to content

Fix ReDim Preserve on property generating invalid ref code#22

Merged
GrahamTheCoder merged 3 commits intomasterfrom
fix-redim-preserve-property-1508587937501568863
Feb 28, 2026
Merged

Fix ReDim Preserve on property generating invalid ref code#22
GrahamTheCoder merged 3 commits intomasterfrom
fix-redim-preserve-property-1508587937501568863

Conversation

@GrahamTheCoder
Copy link
Owner

Fixes an issue where ReDim Preserve on a property resulted in invalid C# code (CS0206). The converter now generates a temporary variable to handle the ref passing required by Array.Resize.


PR created automatically by Jules for task 1508587937501568863 started by @GrahamTheCoder

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>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a critical bug in the VB.NET to C# code converter where ReDim Preserve operations on properties resulted in C# code that failed to compile. The solution ensures that such conversions now produce semantically correct and compilable C# by properly managing array resizing through a temporary variable.

Highlights

  • Fix for ReDim Preserve on properties: Resolved an issue where ReDim Preserve applied to a property in VB.NET would generate invalid C# code (CS0206) during conversion.
  • Improved code generation for array resizing: Modified the converter to introduce a temporary variable when handling ReDim Preserve on properties, ensuring Array.Resize is called correctly with ref passing, thus producing valid C#.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs
    • Introduced a new conditional block to specifically handle ReDim Preserve when the target expression is a member access or an identifier representing a property.
    • Implemented logic to create a temporary local variable, assign the property's array to it, call Array.Resize with ref on the temporary variable, and then assign the modified temporary variable back to the property.
  • Tests/CSharp/StatementTests/RedimPreserveTests.cs
    • Added a new test file RedimPreserveTests.cs to the test suite.
    • Included a RedimPreserveOnPropertyAsync test case to validate the fix for ReDim Preserve on properties, ensuring the generated C# code is correct.
Activity
  • PR created automatically by Jules for task 1508587937501568863, initiated by @GrahamTheCoder.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request fixes an issue where ReDim Preserve on a property was generating invalid C# code. The fix correctly introduces a temporary variable to allow calling Array.Resize, which requires a ref parameter. The included test case verifies the fix for a property access.

My review found one area for improvement in the implementation. The condition to detect a property is too broad and can lead to generating less efficient code for field accesses. I've suggested a more precise way to identify properties, which will make the conversion logic more robust.

var csTargetArrayExpression = await node.Expression.AcceptAsync<ExpressionSyntax>(_expressionVisitor);
var convertedBounds = (await CommonConversions.ConvertArrayBoundsAsync(node.ArrayBounds)).Sizes.ToList();
if (preserve && convertedBounds.Count == 1) {
if (node.Expression is VBSyntax.MemberAccessExpressionSyntax || node.Expression is VBSyntax.IdentifierNameSyntax identifier && _semanticModel.GetSymbolInfo(identifier).Symbol.IsKind(SymbolKind.Property)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The condition to detect if the expression is a property is a bit too broad. node.Expression is VBSyntax.MemberAccessExpressionSyntax will be true for field accesses as well, leading to the generation of unnecessary temporary variables. While the generated code is correct, it's less efficient than passing the field directly by ref.

You can simplify and correct this by checking if the symbol for the expression is a property, which covers both MemberAccessExpressionSyntax and IdentifierNameSyntax.

            if (_semanticModel.GetSymbolInfo(node.Expression).Symbol?.IsKind(SymbolKind.Property) == true) {

google-labs-jules bot and others added 2 commits February 28, 2026 12:59
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>
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>
@GrahamTheCoder GrahamTheCoder merged commit cace870 into master Feb 28, 2026
1 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant