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
Labels
compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion

Comments

@mrmonday
Copy link
Contributor

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).

mrmonday added a commit to mrmonday/CodeConverter that referenced this issue Nov 15, 2019
@GrahamTheCoder GrahamTheCoder added compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion labels Nov 15, 2019
@GrahamTheCoder
Copy link
Member

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
Copy link
Contributor

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
Labels
compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants