Skip to content
New issue

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

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

Already on GitHub? # to your account

VB -> C#: externally declared range variable in For loop can require type conversion #609

Closed
jamesmanning opened this issue Aug 14, 2020 · 2 comments
Labels
VB -> C# Specific to VB -> C# conversion

Comments

@jamesmanning
Copy link

Input code

    Sub Main()
        Dim foo As Single = 3.5
        Dim index As Integer
        For index = Int(foo) To Int(foo * 3)
            Console.WriteLine(index)
        Next
    End Sub

Erroneous output

        public static void Main()
        {
            float foo = 3.5F;
            int index;
            var loopTo = Conversion.Int(foo * 3);
            for (index = Conversion.Int(foo); index <= loopTo; index++)
                Console.WriteLine(index);
        }

Expected output

since the Int() call returns float instead of integer (odd, but I'm not that familiar with VB so maybe it's a compat thing) , we need to cast it to integer so it can get assigned to the existing integer variable from outer scope

        public static void Main()
        {
            float foo = 3.5F;
            int index;
            var loopTo = Conversion.Int(foo * 3);
            for (index = (int)Conversion.Int(foo); index <= loopTo; index++)
                Console.WriteLine(index);
        }

Details

  • VS extension 8.1.6.0
@jamesmanning jamesmanning added the VB -> C# Specific to VB -> C# conversion label Aug 14, 2020
@GrahamTheCoder
Copy link
Member

Ah yes, you're exactly right it's the fact the variable is declared separately that's throwing off the current code getting the type info. Should be an easy fix.

@jamesmanning
Copy link
Author

Awesome, thank you for such a quick fix! You rock @GrahamTheCoder ! 👍

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

No branches or pull requests

2 participants