Casting a boolean to an integer returns -1 for true?

前端 未结 9 809
醉梦人生
醉梦人生 2020-12-01 05:30

I am working with some VB.NET code that seems to be casting a boolean value to an integer using CInt(myBoolean). The odd thing that is happening is that it retu

9条回答
  •  天命终不由人
    2020-12-01 05:59

    It seems like a gotcha, and I don't know any other examples of this behaviour.

    http://msdn.microsoft.com/en-us/library/ae382yt8.aspx specifies this behaviour, with a "Don't do that, mkay" sorta remark with it. Do note further down:

    Conversion in the Framework

    The ToInt32 method of the Convert class in the System namespace converts True to +1.

    If you must convert a Boolean value to a numeric data type, be careful about which conversion method you use.

提交回复
热议问题