CodeConverter icon indicating copy to clipboard operation
CodeConverter copied to clipboard

VB -> C#: Object initializers referencing each other cause conversion error

Open GrahamTheCoder opened this issue 11 months ago • 0 comments

VB.Net input code

Imports System.Drawing

Public Class VisualBasicClass
    Public Sub M()
        Dim Shape = New Point() With {
        .X = 1,
        .Y = .X
        }
    End Sub
End Class

Erroneous output

using System.Drawing;

public partial class VisualBasicClass
{
    public void M()
    {
        Point @init = new Point();
        var Shape = (@init.X = 1, @init.Y = default
#error Cannot convert MemberAccessExpressionSyntax - see comment for details
  /* Cannot convert MemberAccessExpressionSyntax, System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
      at System.ThrowHelper.ThrowKeyNotFoundException()
      at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
      at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitMemberAccessExpression>d__53.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 435
   --- End of stack trace from previous location where exception was thrown ---
      at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
      at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 40

   Input:
   .X

    */, @init).@init;
    }
}System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at ICSharpCode.CodeConverter.CSharp.ExpressionNodeVisitor.<VisitMemberAccessExpression>d__53.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\ExpressionNodeVisitor.cs:line 435
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ICSharpCode.CodeConverter.CSharp.CommentConvertingVisitorWrapper.<ConvertHandledAsync>d__12`1.MoveNext() in C:\Users\gph77\source\repos\CodeConverter\CodeConverter\CSharp\CommentConvertingVisitorWrapper.cs:line 40

Expected output

using System.Drawing;

public partial class VisualBasicClass
{
    public void M()
    {
        Point @init = new Point();
        var Shape = (@init.X = 1, @init.Y = @init.X, @init).@init;
    }
}

Details

  • Product in use: 18e8526e73bc2ae6a0a904e4dabf15ba362ac5e6 new unit test

This is due to only handling one of the types of collection initializers

GrahamTheCoder avatar Feb 24 '24 18:02 GrahamTheCoder