Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

System.String initialisers of the form "New String("Foo")" rejected #32

Open
rolfbjarne opened this issue May 22, 2018 · 0 comments
Open

Comments

@rolfbjarne
Copy link
Member

rolfbjarne commented May 22, 2018

Submitted by a community member on 2012-09-27 07:38 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

Xamarin Bugzilla comment 1 by Rolf Bjarne Kvinge [MSFT] on 2012-09-27 07:56 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"

Reference: https://bugzilla.xamarin.com/show_bug.cgi?id=7496

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant