Discussion:
boolean field : true false and undetermined?
(too old to reply)
HanslH
2006-06-24 13:27:44 UTC
Permalink
Can't find the right terms to google myself to the answer to this
simple question :(..

delphi
clipper cdxdbf files


If I probe a boolean field like
c := db.fieldbyname('boolfield').asstring;
I get records showing as 'true',' false' and '' (empty string)

If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';

they just come up as true or false

Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
HanslH
2006-06-24 13:34:42 UTC
Permalink
..apart from checking for the converted to string content of the
field..
Post by HanslH
Can't find the right terms to google myself to the answer to this
simple question :(..
delphi
clipper cdxdbf files
If I probe a boolean field like
c := db.fieldbyname('boolfield').asstring;
I get records showing as 'true',' false' and '' (empty string)
If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';
they just come up as true or false
Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
Maarten Wiltink
2006-06-24 15:21:46 UTC
Permalink
"HanslH" <***@nospam.nospam> wrote in message news:***@4ax.com...
[...]
Post by HanslH
If I probe a boolean field like
c := db.fieldbyname('boolfield').asstring;
I get records showing as 'true',' false' and '' (empty string)
If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';
they just come up as true or false
Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
You use Field.Value. It returns a variant, which can be true, false, or
null for a Boolean field. AsBoolean apparently returns false for null.

Groetjes,
Maarten Wiltink
Bruce Roberts
2006-06-24 16:15:02 UTC
Permalink
Post by HanslH
Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
Look of the definition of Boolean. The type only has two possible values,
True and False. If you need three potential values, use an enumnerated
type:

Type tTriState = (tsFalse, tsTrue, tsMaybe);

Databases sometimes maintain an "uninitialized" state for record fields.
This should not be confused with valid values for the field type. In Delphi
you can test the Variant value of a field for Null. Sometimes a True result
will mean that the field has not been initialized, but sometimes it simply
means that the field contents equate to a null value, e.g. empty string
fields.

I would suggest that one should always attempt to design code so that only
type valid values exist in db fields. Testing for "undetermined" content
is, to my mind, much like trying to test for pointer validity - its not
really possible to do so reliably.
HanslH
2006-06-29 13:27:04 UTC
Permalink
On Sat, 24 Jun 2006 12:15:02 -0400, "Bruce Roberts"
Post by Bruce Roberts
Post by HanslH
Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
Look of the definition of Boolean. The type only has two possible values,
True and False. If you need three potential values, use an enumnerated
Type tTriState = (tsFalse, tsTrue, tsMaybe);
Databases sometimes maintain an "uninitialized" state for record fields.
This should not be confused with valid values for the field type. In Delphi
you can test the Variant value of a field for Null. Sometimes a True result
will mean that the field has not been initialized, but sometimes it simply
means that the field contents equate to a null value, e.g. empty string
fields.
I would suggest that one should always attempt to design code so that only
type valid values exist in db fields. Testing for "undetermined" content
is, to my mind, much like trying to test for pointer validity - its not
really possible to do so reliably.
Which diminishes the number of times a boolean field is applicable
considerably..
Just out of curiosity..
A tdbcheckbox component can have the option allowgreyed=true; if you
link it to a boolean field i would expect to have the greyed result
relate to field result null, so there is some official status to the
null result of a boolean field..
Bruce Roberts
2006-06-29 15:07:39 UTC
Permalink
Post by HanslH
Which diminishes the number of times a boolean field is applicable
considerably..
Perhaps, but specialized tools are not necessarily bad. If one can reliably
count on Boolean only ever having two values then code can be simpler and
much more robust. If one truely needs three states, then one can use an
appropriate data type.
Post by HanslH
Just out of curiosity..
A tdbcheckbox component can have the option allowgreyed=true; if you
link it to a boolean field i would expect to have the greyed result
relate to field result null, so there is some official status to the
null result of a boolean field..
I'm not sure what you mean by official. The point I was trying to make is
that while some db may support a "uninitialized" state for fields this is
not universally so. Therefore code that may be used with different db must
be crafted more carefully.

Jamie
2006-06-25 16:43:36 UTC
Permalink
Post by HanslH
Can't find the right terms to google myself to the answer to this
simple question :(..
delphi
clipper cdxdbf files
If I probe a boolean field like
c := db.fieldbyname('boolfield').asstring;
I get records showing as 'true',' false' and '' (empty string)
If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';
they just come up as true or false
Since I use 'boolean fields' that have to be true/false/undetermined a
lot i'v grown accustomed to making ( one) char- in stead of boolean
fields but how do I test a boolean field in a record for undetermined
content?
false is the same as none.
--
Real Programmers Do things like this.
http://webpages.charter.net/jamie_5
Maarten Wiltink
2006-06-25 18:44:09 UTC
Permalink
"Jamie" <***@charter.net> wrote in message news:J7wng.3366$***@fe04.lga...
[...]
Post by Jamie
false is the same as none.
That is patently untrue. The Boolean values are False and True. Null is
not a Boolean value. A pointer to a Boolean may point to True, or point
to False, or be nil. The null pointer does not point to False.

Groetjes,
Maarten Wiltink
Marc Rohloff
2006-06-26 12:20:45 UTC
Permalink
Post by HanslH
Can't find the right terms to google myself to the answer to this
simple question :(..
If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';
AsBoolean always returns a default value for 'null' (AsInteger is the
same, it returns 0 and AsString returns '' which is not the saem as
null).
You need to test explitictly using IsNull

if fieldbyname('boolfield').isnull then
c := ''
else if (fieldbyname('boolfield').asboolean) then
c := 'true'
else
c:= 'false';

--
Marc
HanslH
2006-06-29 11:56:55 UTC
Permalink
thnx, this was the answer i was looking for

On 26 Jun 2006 05:20:45 -0700, "Marc Rohloff"
Post by Marc Rohloff
Post by HanslH
Can't find the right terms to google myself to the answer to this
simple question :(..
If on the other hand say
if fieldbyname('boolfield').asboolean then
c := 'true'
else if (not fieldbyname('boolfield').asboolean) then
c := 'false'
else
c:= '';
AsBoolean always returns a default value for 'null' (AsInteger is the
same, it returns 0 and AsString returns '' which is not the saem as
null).
You need to test explitictly using IsNull
if fieldbyname('boolfield').isnull then
c := ''
else if (fieldbyname('boolfield').asboolean) then
c := 'true'
else
c:= 'false';
Continue reading on narkive:
Search results for 'boolean field : true false and undetermined?' (Questions and Answers)
3
replies
How do I eliminate boolean filed from SQL statement?
started 2012-04-28 13:25:32 UTC
programming & design
Loading...