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#: Error with multiple variable declarations with implicit type #373

Closed
mrmonday opened this issue Sep 6, 2019 · 4 comments
Closed
Labels
compilation error A bug where the converted output won't compile good first issue VB -> C# Specific to VB -> C# conversion

Comments

@mrmonday
Copy link
Contributor

mrmonday commented Sep 6, 2019

Input code

Friend Module Module1
    Sub Main()
        Dim x, y As Date
        Console.WriteLine(x)
        Console.WriteLine(y)
    End Sub
End Module

Erroneous output

using System;

namespace ConsoleApp3
{
    internal static class Module1
    {
        public static void Main()
        {
            var x = default(DateTime), y = default(DateTime);
            Console.WriteLine(x);
            Console.WriteLine(y);
        }
    }
}

Expected output

using System;

namespace ConsoleApp3
{
    internal static class Module1
    {
        public static void Main()
        {
            DateTime x = default(DateTime), y = default(DateTime);
            Console.WriteLine(x);
            Console.WriteLine(y);
        }
    }
}

Alternatively, this could be split into two declarations.

Details

Error:

error CS0819: Implicitly-typed variables cannot have multiple declarators

Version used: master @ 3ffc597

@GrahamTheCoder GrahamTheCoder added compilation error A bug where the converted output won't compile VB -> C# Specific to VB -> C# conversion labels Sep 10, 2019
@BrianFreemanAtlanta
Copy link
Contributor

In looking at the documentation here:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/variables/variable-declaration
I believe that in VB the statement

Dim x, y as Date

both x and y are strongly typed as Date variable type so the expected output would be

using System;

namespace ConsoleApp3
{
    internal static class Module1
    {
        public static void Main()
        {
            DateTime x = default(DateTime);
            DateTime y = default(DateTime);
            Console.WriteLine(x);
            Console.WriteLine(y);
        }
    }
}

Because the intent of both x and y being typed to DateTime and not var are then being met. In addition when testing the first option with "," separator in C# is not valid syntax so a 2 line declaration is needed.

So I believe for this issue the expected output should be revised.

@GrahamTheCoder
Copy link
Member

Thanks for your interest. You're correct that both x and y should be DateTimes. Both the original, and your suggestion achieve that, and are acceptable conversions. I'd happily accept a PR for either, with a marginal preference for the one mentioned in the current issue description.
This is simply because it looks closer to the VB and is equal in all other respects I can think of such as these key points.

Here's a runnable example that shows the syntax is valid:
https://dotnetfiddle.net/lyNSIR

@BrianFreemanAtlanta
Copy link
Contributor

Yes, you are correct. I'm not sure why my system was flagging the original code as bad, but it's taking it now. I'm working on learning the ins and outs of Rosalyn. Will submit PR or update if I can get my head wrapped around it.

@GrahamTheCoder
Copy link
Member

Great to hear. I think the relevant method is called SplitVariableDeclarations because it is used for both local variables and fields. Usually var is preferred (except for function types), but we need to tell the type syntax generator method not to use var on multi-declarator cases too. Note that in the case of fields they do have to be split in c# (and already are I believe).

Feel free to ask questions or submit partial prs to have discussion around.

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 good first issue VB -> C# Specific to VB -> C# conversion
Projects
None yet
Development

No branches or pull requests

3 participants