(true = 1) returns false?

B

Bob Barrows [MVP]

Jason said:
I don't think you understand the problem. ASP defines 0 as false and
-1 as true, and everything else is neither.

No, not quite. If an expression evaluates to 0, it is False. All other
results are True*. Again, try:

<%
Response.Write CBool(-1)
Response.Write CBool(0)
Response.Write CBool(1)
Response.Write CBool(2)

%>
Here is the result I get from this:
TrueFalseTrueTrue
So you see that CBool(2) resulted in True. In fact, the only one that
resulted in False was 0.

In fact, try this:
<%
if (1+3) then
Response.Write "<br>true"
else
Response.Write "<br>false"
end if

s="abcd"
Response.Write "<BR>" & instr(s,"c")
if instr(s,"c") then
Response.Write "<BR>c is in abcd"
else
Response.Write "<BR>c is not in abcd"
end if
%>


This is the real reason both Yes/No values from Access and bit values
from SQL Server work well in vbscript/vb.
I'm still not clear about why your boolean values from MySQL aren't
working the same way.

Bob Barrows

*I realize I mistakenly said the reverse of this in my earlier message -
my apologies.
 
R

roger

Yes. That's exactly what I want, but your example doesn't work that way for
me when I do it. For me, b = 0 evaluates to false, b = -1 evaluates to
true, and if b equals any other number, then b is neither true nor false.
Are you sure that code works for you? I mean, did you test it? Because if
it does, I would be interested to know why it works for you and not for
me.

Very strange. And yes I did test it.

"if b then"
should test whether b is non-zero.

and

"if b = true then"
should test whether b is identical to the definition of the keyword "true",
which in VBScript is -1.

I remain mystified why this doesn't work for you.
 
A

Anthony Jones

Bob Barrows said:
No, not quite. If an expression evaluates to 0, it is False. All other
results are True*. Again, try:

<%
Response.Write CBool(-1)
Response.Write CBool(0)
Response.Write CBool(1)
Response.Write CBool(2)

%>
Here is the result I get from this:
TrueFalseTrueTrue
So you see that CBool(2) resulted in True. In fact, the only one that
resulted in False was 0.

In fact, try this:
<%
if (1+3) then
Response.Write "<br>true"
else
Response.Write "<br>false"
end if

s="abcd"
Response.Write "<BR>" & instr(s,"c")
if instr(s,"c") then
Response.Write "<BR>c is in abcd"
else
Response.Write "<BR>c is not in abcd"
end if
%>


This is the real reason both Yes/No values from Access and bit values
from SQL Server work well in vbscript/vb.
I'm still not clear about why your boolean values from MySQL aren't
working the same way.

Actually the reason it works is that if you check the field type property it
is set to adBoolean (11) that is boolean.
Since the value property is a variant it will contain either -1 for True or
0 for False.

I suspect the MySQL isn't using this adBoolean (probably adUnsignedTinyInt
or some such) and therefore the value comes through as 1
 
B

Bob Barrows [MVP]

Anthony said:
Actually the reason it works is that if you check the field type
property it is set to adBoolean (11) that is boolean.
Since the value property is a variant it will contain either -1 for
True or 0 for False.

I suspect the MySQL isn't using this adBoolean (probably
adUnsignedTinyInt or some such) and therefore the value comes through
as 1
Yes, you're probably correct.
 
J

Jason

Bob Barrows said:
This is the real reason both Yes/No values from Access and bit values
from SQL Server work well in vbscript/vb.
I'm still not clear about why your boolean values from MySQL aren't
working the same way.

Bob Barrows
I'm not clear why either. :)

--jason
 
J

Jason

Bob Barrows said:
Yes, you're probably correct.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Ok, I figured out why we're having this problem. And it's kind of my bad,
kind of. :p I assumed it would work the same way it does in C++, but it
doesn't.

My if statements look like this:

<%
if rsobj("column") = true then
%>

and that fails, but if I change them to this:

<%
if rsobj("colum") then
%>

then it works.

It appears that true is just an alias for -1. So ASP (using vbscript) as a
language defines true as anything except false, and defines false as zero,
but the keyword true is only defined as -1.

I can't say I am pleased with this, but I am pleased with knowing why.

--jason
 
A

Anthony Jones

Jason said:
Ok, I figured out why we're having this problem. And it's kind of my bad,
kind of. :p I assumed it would work the same way it does in C++, but it
doesn't.

My if statements look like this:

<%
if rsobj("column") = true then
%>

and that fails, but if I change them to this:

<%
if rsobj("colum") then
%>

then it works.

It appears that true is just an alias for -1. So ASP (using vbscript) as a
language defines true as anything except false, and defines false as zero,
but the keyword true is only defined as -1.

I can't say I am pleased with this, but I am pleased with knowing why.


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
 
B

Bob Barrows [MVP]

Anthony said:
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
An alternative would be to coerce the MySQL boolean trues to -1, perhaps
using a view with a calculated column.
 
J

Jason

Anthony Jones said:
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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top