Re: (true = 1) returns false? -- THE ANSWER
"Anthony Jones" <Ant@yadayadaya da.com> wrote in message
news:u3fH6TtiGH A.1508@TK2MSFTN GP04.phx.gbl...[color=blue]
>
> Take this C++ :-
>
> if (5 == true)
> // 5 is equal to true this doesn't happen
> else
> // 5 is not equal to true this happens
>
>
> However things are different if you do this:-
>
> if (1 == true)
> // 1 is equal to true this does happen
> else
> // 1 is not equal to true this doesn't happen
>
> or this:-
>
> if (5)
> // This always happens
> else
> // This never happens
>
>
> In C/C++ (as is the same with VB/Script) when a boolean type and a numeric
> type are either side of an operand it is the boolean which is coerced to a
> numeric. However the C/C++ true is an 'alias' for 1 whereas in
> VB/Script/COM a true is -1. The reason -1 was chosen is that VB does not
> differentiate between bitwise and logical operators (e.g. & or && in C)
> it only has bitwise operators (and, or, not).
>
> It seems you have two problems.
>
> 1) using 'if x = true then' forces the true to be coerced to a numeric and
> then a comparison is made and then a branch is made accordingly. Whereas
> 'if x Then' simply branches to 'then' on non-zero and 'else' on zero.
>
> 2) rsobj("column") isn't a boolean type but some kind of numeric type
> (although I have found some interface declare a COM interface having a
> boolean parameter or member but actually use 1 instead of -1 but that is
> poor implementation) .
>
> Frankly stop doing this x = true and be aware that all apparently
> logical
> operators in VB are in fact bitwise
>[/color]
Yeah, you are right. I assumed 'if b == true then...' and 'if b then...'
were identical statements, but they aren't. Your explanantion of how the
bool is casted as an int helps to make it clear as to why it happens because
I did not really know why.
--jason
"Anthony Jones" <Ant@yadayadaya da.com> wrote in message
news:u3fH6TtiGH A.1508@TK2MSFTN GP04.phx.gbl...[color=blue]
>
> Take this C++ :-
>
> if (5 == true)
> // 5 is equal to true this doesn't happen
> else
> // 5 is not equal to true this happens
>
>
> However things are different if you do this:-
>
> if (1 == true)
> // 1 is equal to true this does happen
> else
> // 1 is not equal to true this doesn't happen
>
> or this:-
>
> if (5)
> // This always happens
> else
> // This never happens
>
>
> In C/C++ (as is the same with VB/Script) when a boolean type and a numeric
> type are either side of an operand it is the boolean which is coerced to a
> numeric. However the C/C++ true is an 'alias' for 1 whereas in
> VB/Script/COM a true is -1. The reason -1 was chosen is that VB does not
> differentiate between bitwise and logical operators (e.g. & or && in C)
> it only has bitwise operators (and, or, not).
>
> It seems you have two problems.
>
> 1) using 'if x = true then' forces the true to be coerced to a numeric and
> then a comparison is made and then a branch is made accordingly. Whereas
> 'if x Then' simply branches to 'then' on non-zero and 'else' on zero.
>
> 2) rsobj("column") isn't a boolean type but some kind of numeric type
> (although I have found some interface declare a COM interface having a
> boolean parameter or member but actually use 1 instead of -1 but that is
> poor implementation) .
>
> Frankly stop doing this x = true and be aware that all apparently
> logical
> operators in VB are in fact bitwise
>[/color]
Yeah, you are right. I assumed 'if b == true then...' and 'if b then...'
were identical statements, but they aren't. Your explanantion of how the
bool is casted as an int helps to make it clear as to why it happens because
I did not really know why.
--jason
Comment