Bug 7496 - System.String initialisers of the form "New String("Foo")" rejected
Summary: System.String initialisers of the form "New String("Foo")" rejected
Alias: None
Product: Compilers
Classification: Mono
Component: VisualBasic ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Rolf Bjarne Kvinge [MSFT]
Depends on:
Reported: 2012-09-27 07:38 UTC by Lee Millward
Modified: 2018-05-22 12:13 UTC (History)
2 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Lee Millward 2012-09-27 07:38:14 UTC
The following code snippet fails to compile with VBNC, but is successfully compiled by VBC:

Public Class SomeClass

    Private Foo as new String("Init")

End Class

VBNC produces the following errors when compiling the above:

error VBNC30519: Overload resolution failed because no accessible '.ctor' can be called without a narrowing conversion:
    'Public Sub New((value As Char))': Argument matching parameter 'value' narrows from 'String' to 'Char'.
    'Public Sub New((value As SByte))': Argument matching parameter 'value' narrows from 'String' to 'SByte'.
    'Public Sub New((value() As Char))': Argument matching parameter 'value' narrows from 'String' to 'System.Char[]'.

Compiling the code with VBC and looking at the diassembled code, VBC turns the above into the following:

Imports Microsoft.VisualBasic.CompilerServices
Imports System
Public Class SomeClass

    Private Foo As String

    Public Sub New()
        Me.Foo = New String(Conversions.ToCharArrayRankOne("Init"))
    End Sub

End Class
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-09-27 07:56:30 UTC
This has already been reported here: https://bugzilla.novell.com/show_bug.cgi?id=392464

The workaround is easy, just not create the string explicitly:

Private Foo As String = "Init"
Comment 2 Rolf Bjarne Kvinge [MSFT] 2018-05-22 12:13:41 UTC
Moved to: https://github.com/mono/mono-basic/issues/32