icsharpcode / CodeConverter

Convert code from C# to VB.NET and vice versa using Roslyn
https://icsharpcode.github.io/CodeConverter/
MIT License
842 stars 219 forks source link

VB -> C#: ReDim Preserve of array property #1156

Open gaschd opened 2 weeks ago

gaschd commented 2 weeks ago

Converting ReDim Preserve on an array property throws CS0206: A property or indexer may not be passed as an out or ref parameter while Array.Resize is converted correctly by utilizing a local variable for this case.

VB.Net input code

Public Class TestClass
    Public Property NumArray1 As Integer()

    Public Sub New()
        Array.Resize(NumArray1, 3)

        ReDim Preserve NumArray1(4)
    End Sub
End Class

Erroneous output

using System;

public partial class TestClass
{
public int[] NumArray1 { get; set; }

public TestClass()
{
    var argarray = NumArray1;
    Array.Resize(ref argarray, 3);
    NumArray1 = argarray;

    Array.Resize(ref this.NumArray1, 5);
}

Expected output

using System;

public partial class TestClass
{
    public int[] NumArray1 { get; set; }    

    public TestClass()
    {
        var argarr = NumArray1;
        Array.Resize(ref argarr, 3);
        NumArray1 = argarr;

        var argarr1 = NumArray1;
        Array.Resize(ref argarr1, 5);
        NumArray1 = argarr1;
    }
}

Details