Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

VB -> C#: Converted With block does not update original struct element or property #634

Closed
eveltman-twinfield opened this issue Sep 24, 2020 · 3 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@eveltman-twinfield
Copy link

Input code

Public Structure SomeStruct
    Public FieldA As String
End Structure

Module Module1
   Sub Main()
      Dim myArray(0) As SomeStruct

      With myArray(0)
         .FieldA = "New FieldA value"
      End With

      'Outputs: FieldA was changed to New FieldA value 
      Console.WriteLine($"FieldA was changed to {myArray(0).FieldA}")
      Console.ReadLine
   End Sub
End Module

Erroneous output

public partial struct SomeStruct
{
    public string FieldA;
}

internal static partial class Module1
{
    public static void Main()
    {
        var myArray = new SomeStruct[1];
        {
            var withBlock = myArray[0];
            withBlock.FieldA = "New FieldA value";
        }

        // Outputs: FieldA was changed to
        Console.WriteLine($"FieldA was changed to {myArray[0].FieldA}");
        Console.ReadLine();
    }
}

Expected output

Not sure what the best solution is here.
I was about to suggest copying the withBlock variable back to the original element at the end.
But if the structure has methods that are called somewhere halfway the With block, then that method would be operating on a copy of the structure so the converted code would still behave differently from the original.

Alternatively, the With block could be converted to a local method, with the original element passed by reference, but I was struggling to get the syntax right for that. Maybe I was trying something that C# doesn't support (or I just was too much in a hurry).

Details

  • Simple object expressions work fine, because in those cases the converter does not introduce a withBlock variable, and assignments within the block keep operating directly on the 'subject' of the original With block.
  • The issue is also occurring for structs that are members of a class (With someObject.SomeStructure).
  • I didn't try with other value types but maybe the same issue occurs there too.
  • Issue first found in the VS extension, and reproduced with codeconverter.icsharpcode.net.
  • Version in use: 8.1.8.0.
@eveltman-twinfield eveltman-twinfield added the VB -> C# Specific to VB -> C# conversion label Sep 24, 2020
@GrahamTheCoder
Copy link
Member

Thanks for this. Mutating structs is a subtle area!
I'm a bit busy at the moment so it might be a while before I have space to dig into this one. I'm sure there must be a way though ☺

@GrahamTheCoder
Copy link
Member

See #716 for more example cases

@eveltman-twinfield
Copy link
Author

Great, thanks for the fix! I haven't converted VB in a while, but it's nice to see next time I won't run into this issue anymore :)

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants