dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Declaring multiple variables with "As New" on a single line does not initialize them all #4700

Closed gafter closed 9 years ago

gafter commented 9 years ago

declarations such as:

Private bw1, bw2, bw3, bw4 As New BackgroundWorker()

only creates an instance for bw4 and not its predecessors

[@gafter: I believe this problem occurs for field declarations]

[Migrated from TFS/DevDiv 1197285]

gafter commented 9 years ago

Here is a test for this

Imports System.Console ' works
Imports A.B ' does not work

Public Class A
    Dim x1, x2, x3 As New X()
    Public Overrides Function ToString() As String
        Dim x4, x5, x6 As New X()
        Return $"{x1} {x2} {x3} {x4} {x5} {x6}"
    End Function
End Class

Public Class X
    Dim value As Integer
    Public Sub New()
        Me.value = NextValue()
    End Sub
    Shared Function NextValue()
        Static [next] As Integer
        Return Threading.Interlocked.Increment([next])
    End Function
    Public Overrides Function ToString() As String
        Return value.ToString()
    End Function
End Class

Module Module1

    Sub Main()
        Dim a = New A
        Console.WriteLine(a.ToString())
    End Sub

End Module

It appears to work as intended in VS2015 RTM.