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#: Language version seems to be ignored in some cases (C# 7 feature is used in C# 6 conversion request) #878

Closed
N-Olbert opened this issue Apr 14, 2022 · 2 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@N-Olbert
Copy link

VB.Net input code

Public Function Foo(x As Decimal) As Decimal
    If (x > "0") Then
	    Return x * "30,56" 
    End If
    Return Nothing 
End Function

Erroneous output

public decimal Foo(decimal x)
{
    if ((double)x > Conversions.ToDouble("0"))
    {
        return (decimal)((double)x * Conversions.ToDouble("30,55"));
    }

    return default; // NOTE: default is a C# 7 feature
}

Expected output

public decimal Foo(decimal x)
{
    if ((double)x > Conversions.ToDouble("0"))
    {
        return (decimal)((double)x * Conversions.ToDouble("30,55"));
    }

    return default(decimal); // NOTE: default(decimal) (this works in C# 6)
}

Converter code

var conversionOptions = new CodeWithOptions(code)
                                                .SetFromLanguage(LanguageNames.VisualBasic, 14)
                                                .SetToLanguage(LanguageNames.CSharp, 6) // NOTE: Language version is set to 6
                                                .WithTypeReferences(references);
var conversationResult = await CodeConverter.ConvertAsync(conversionOptions)

Details

  • Product in use: NuGet-package "ICSharpCode.CodeConverter"
  • Version in use: 8.5.0.374
@N-Olbert N-Olbert added the VB -> C# Specific to VB -> C# conversion label Apr 14, 2022
@GrahamTheCoder
Copy link
Member

GrahamTheCoder commented Apr 14, 2022

I'm afraid the option to control the version (especially of the output) has only a small influence - it is not strictly adhered to. It probably should have been deprecated long ago. It's not really feasible to support multiple different language versions elegantly unfortunately. I'll leave this issue open to track deprecating the options so as not to mislead people!

What I'd recommend is to use a c#code formatter to reformat the result to your preferred style

@GrahamTheCoder
Copy link
Member

Options will be removed in the next release (which will be a major version increment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

2 participants