Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VB -> C#: Incorrect visibility for constructor #422

Closed
mrmonday opened this issue Nov 14, 2019 · 2 comments · Fixed by #425
Closed

VB -> C#: Incorrect visibility for constructor #422

mrmonday opened this issue Nov 14, 2019 · 2 comments · Fixed by #425

Comments

@mrmonday
Copy link
Contributor

@mrmonday mrmonday commented Nov 14, 2019

Input code

Class Class1
    Sub New(x As Boolean)
    End Sub
End Class

Erroneous output

class Class1
{
    Class1(bool x)
    {
    }
}

Expected output

class Class1
{
    public Class1(bool x)
    {
    }
}

Details

  • Version in use: 7.3.0

Visibility of constructors in visual basic is public when no other modifier is specified (can't find this in the spec, but the same happens for other methods, and ILSpy confirms).

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

@GrahamTheCoder GrahamTheCoder commented Nov 15, 2019

I previously tried a global approach to #349 and hit some issues (the CsSyntaxGenerator isn't as far reaching as I'd hoped), but it might be applicable here. I suspect a sensible model for these things to follow in some cases might be to use the syntaxgenerator, then replace bits using "With..." methods.

@BrianFreemanAtlanta

This comment has been minimized.

Copy link
Contributor

@BrianFreemanAtlanta BrianFreemanAtlanta commented Nov 16, 2019

This specific issue is related to the difference in default visibility between c# and VB. I'll add this so there is a reference to the relevant MSDN docs:

VB overview

Excerpt:

Public access is the normal level for a programming element when you do not need to limit access to it. Note that the access level of an element declared within an interface, module, class, or structure defaults to Public if you do not declare it otherwise.

Here is the full breakdown for VB

Here is overview for C#
and the full breakdown of access modifiers
excerpt:

Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.

and

Struct members, including nested classes and structs, can be declared as public, internal, or private. Class members, including nested classes and structs, can be public, protected internal, protected, internal, private protected or private. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type.

So the default visibility for C# class members is private while VB default is public. This leads us to have to add an access modifier to keep the same behavior.

GrahamTheCoder added a commit that referenced this issue Nov 17, 2019
Ensure class constructors are marked as public [#422]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.