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#: CBool() conversion incorrect #327

Closed
mrmonday opened this issue Jul 18, 2019 · 2 comments

Comments

@mrmonday
Copy link
Contributor

commented Jul 18, 2019

Input code

Imports System

Namespace ConsoleApp4
    Friend Class Program
        Public Shared Sub Main(ByVal args As String())
            Dim x As Object = "-1"
            Console.WriteLine(System.Convert.ToBoolean(x))
            Console.Read()
        End Sub
    End Class
End Namespace

Prints True.

IL:

	IL_0000: nop
	IL_0001: ldstr "-1"
	IL_0006: stloc.0
	IL_0007: ldloc.0
	IL_0008: call bool [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.Conversions::ToBoolean(object)
	IL_000d: call void [mscorlib]System.Console::WriteLine(bool)
	IL_0012: nop
	IL_0013: call int32 [mscorlib]System.Console::Read()
	IL_0018: pop
	IL_0019: ret

Erroneous output

using System;

namespace ConsoleApp4
{
    namespace ConsoleApp4
    {
        internal class Program
        {
            public static void Main(string[] args)
            {
                object x = "-1";
                Console.WriteLine(System.Convert.ToBoolean(x));
                Console.Read();
            }
        }
    }
}

Which throws an exception:

System.FormatException
  HResult=0x80131537
  Message=String was not recognized as a valid Boolean.
  Source=mscorlib
  StackTrace:
   at System.Boolean.Parse(String value)
   at System.String.System.IConvertible.ToBoolean(IFormatProvider provider)
   at System.Convert.ToBoolean(Object value)
   at ConsoleApp4.ConsoleApp4.Program.Main(String[] args) in C:\Users\robert.clipsham\source\repos\ConsoleApp4\ConsoleApp4\Program.cs:line 12

IL:

	IL_0000: nop
	IL_0001: ldstr "-1"
	IL_0006: stloc.0
	IL_0007: ldloc.0
	IL_0008: call bool [mscorlib]System.Convert::ToBoolean(object)
	IL_000d: call void [mscorlib]System.Console::WriteLine(bool)
	IL_0012: nop
	IL_0013: call int32 [mscorlib]System.Console::Read()
	IL_0018: pop
	IL_0019: ret

Expected output

using System;

namespace ConsoleApp4
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            object x = "-1";
            Console.WriteLine(Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(x));
            Console.Read();
        }
    }
}

Details

Product in use: VS extension

Version in use: 6.9.0.0

There seems to be a duplicate namespace produced too - looking at the IL, that might actually be correct, but I'm not sure it's expected behaviour. Should I open a bug for that?

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

commented Jul 20, 2019

Thanks! I believe the namespace issue is the one covered in #201

@GrahamTheCoder

This comment has been minimized.

Copy link
Member

commented Aug 2, 2019

Consolidating

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.