views:

4153

answers:

6

What will this print, and why?

Sub Main()

    Dim i As Integer
    Dim b As Boolean

    i = 1
    b = i
    i = b
    Console.WriteLine(i)

    i = Convert.ToInt32(b)
    Console.WriteLine(i)

End Sub

I know what it prints (check revision history or run it yourself to see). I'm interested in the "why".

EDIT: (Just a joke :) You can get 0 too...

Int32.TryParse("True", i)
Console.WriteLine(i)
+1  A: 

That's why I use C#. Do you have STRICT turned on or off? You should't be doing

b=i
Otávio Décio
Correct me if I'm wrong, but shouldn't VB.NET type-check at compile time anyway? I thought VB did not, but VB.NET did.
tehblanx
If you turn off STRICT it won't any useful type checking. I hate it with a passion.
Otávio Décio
Strict is Off but you can turn it On and use CBool, CInt with the same result.
Prankster
I'd argue the opposite. One of VB's great features is loose typing and dynamic execution. It's one of the ideas that differentiates it from C#. There's nothing inherently wrong with using VB in a dynamic fashion.
JaredPar
-1 for "That's why I use C#". Groan. I am so sure that you actually did an analysis of type conversion between boolean and int32 and used the results to decide which language to use.
JohnFx
You are all entitled to your own opinions.
Otávio Décio
+6  A: 

Some languages consider boolean true to be -1 rather than 1. I'd have to do research to see why, as I don't recall.

In VB6, the constant True has the value -1.

However, Convert.ToInt32(Boolean) is documented as returning "The number 1 if value is true; otherwise, 0." That way, it's the same no matter which framework language you're using.

Edit: See the question boolean true -- positive 1 or negative 1

R. Bemrose
A: 

That's because in VB.Net, Boolean values are -1 for true and 0 for false by default. Not sure why it prints as 1 the second time, though...

Matt Grande
A: 

From msdn VB docs..

Type Conversions

When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True. When Visual Basic converts Boolean values to numeric types, False becomes 0 and True becomes -1.

and for Convert.ToInt32(value)

retuns the number 1 if value is true; otherwise, 0.

So for your code:

i = 1
b = i // b becomes true
i = b // true = -1
Console.WriteLine(i)  // i is -1

i = Convert.ToInt32(b)  // Convert.ToInt32(true) = 1
Console.WriteLine(i)    // i is 1
Arnold Spence
Add a link for those doc quotes and I'll upmod.
Joel Coehoorn
+7  A: 

What you're seeing is the is a bit of legacy code showing it's head.

At the heart of the matter is the VT_BOOL type. VB6 used the VT_BOOL type (aka VARIANT_BOOL) for its boolean values. True for a VARIANT_BOOL is represented with the value VARIANT_TRUE which has the integer value -1. During the conversion to .Net it was decided that when using VB conversion routines to convert a boolean value to an Integer value, VB6 semantics would be maintained an the return value would be -1.

The first implicit conversion occurs with the b = i line. Under the hood this does an implicit conversion from integer to boolean. Any non-zero value is deemed to be true therefore the resulting value is true.

However, the following line of code is doing an implicit conversion to an integer type.

i = b

Under the hood this uses one of the VB conversion routines (CType or CInt) to convert the value to an integer. As such VB semantics are in play and the value returned is -1.

The next interesting line is Convert.ToInt32() line. This is using a .Net conversion routine which does not use VB semantics. Instead in returns the underlying BCL representation for a true boolean value which is 1.

JaredPar
Scott Wisniewski
That way "true and x" is non zero anytime "x" is non zero.
Scott Wisniewski
+2  A: 

As to why -1 is used for True, I believe that it's because it is literally (NOT 0).

Start with zero, flip all the bits and then read it as two's complement--comes out negative one.

So since anything that is not False is True, and False is 0, the (NOT False) is represented by -1.

This may be just coincidence, though....

RolandTumble
That's exactly why VB uses -1 for True. :)
Daniel15