Skip to content

VB -> C#: variable is initialized inside loop #897

@KunzeAndreas

Description

@KunzeAndreas

VB.Net input code

Private Shared Sub DefaultInitialization()
    For i = 1 To 2
        Dim b As Boolean
        If i = 1 Then
            Assert.That(Not b)
        Else
            Assert.That(b)
        End If

       b = True
       Assert.That(b)
   Next
 End Sub

Erroneous output

private static void DefaultInitialization()
{
    for (int i = 1; i <= 2; i++)
    {
        var b = default(bool);
        if (i == 1)
        {
            Assert.That(!b);
        }
        else
        {
            Assert.That(b);
        }

        b = true;
        Assert.That(b);
    }
}

Expected output

private static void DefaultInitialization()
{
    var b = default(bool);  // define and initialize outside loop or do not initialize (with compile error CS0165)
    for (int i = 1; i <= 2; i++)
    {
        // bool b;
        if (i == 1)
        {
            Assert.That(!b);
        }
        else
        {
            Assert.That(b);
        }

        b = true;
        Assert.That(b);
    }
}

Details

  • Product in use: VS extension
  • Version in use: 9.0.0.0
  • Did you see it working in a previous version, which? no

Metadata

Metadata

Assignees

No one assigned

    Labels

    VB -> C#Specific to VB -> C# conversionoutput logic errorA bug where the converted output behaves differently to the input code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions