Recent

Author Topic: boolean.ToString()  (Read 7166 times)

mobilevil

  • Jr. Member
  • **
  • Posts: 69
    • http://www.kachun.com
boolean.ToString()
« on: August 06, 2018, 12:34:53 pm »
Hello,
my skill is still stuck in delphi 7 days so...

I just found that primitive types now have member functions built in but I wonder why .ToString() of a boolean variable behave this way.

in the code below, 
#1, TUseBoolStrs.True is quite long to type, I expected a simple TRUE here,
#2, I can't think of a use of it. how come a boolean-object member function is returning string based on a boolean input parameter instead of the boolean-object's own value?

var
  mybool:boolean;
begin
  mybool:=False;

//#1
  listbox1.items.add(mybool.ToString(TUseBoolStrs.True); //return 'False'

//#2
  listbox1.items.add(mybool.ToString(True); //return '-1' which is comming from the value parameter instead of mybool 
  listbox1.items.add(mybool.ToString(False); //return '0' which is comming from the value parameter instead of mybool

end;
« Last Edit: August 07, 2018, 04:04:23 am by mobilevil »

Thaddy

  • Hero Member
  • *****
  • Posts: 14375
  • Sensorship about opinions does not belong here.
Re: boolean.ToString()
« Reply #1 on: August 06, 2018, 01:10:55 pm »
Can you give a compilable example, pref. not reliant on Lazarus? I am too lazy to write such a thing myself today (it is 36.2 c in my garden already) like I usually do.
And yes in D7 Boolean true is a signed type returning -1.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mobilevil

  • Jr. Member
  • **
  • Posts: 69
    • http://www.kachun.com
Re: boolean.ToString()
« Reply #2 on: August 06, 2018, 01:20:14 pm »
30+ degree is norm for me and 36 sounds hot! but air-conditioning is standard in Hong Kong so....
maybe it's not a bug but I don't understand why the design of ToString come into this way.

attached a simple console application,

  mytrue:=true;
  myfalse:=false;
  writeln('mybool.ToString(TUseBoolStrs.true):'+mytrue.ToString(TUseBoolStrs.true));
  writeln('myfalse.ToString(TUseBoolStrs.true):'+myfalse.ToString(TUseBoolStrs.true));
  writeln('myfalse.ToString(true):'+myfalse.ToString(true));
  writeln('myfalse.ToString(false):'+myfalse.ToString(false));

and the output is this

mybool.ToString(TUseBoolStrs.true):True
myfalse.ToString(TUseBoolStrs.true):False
myfalse.ToString(true):-1
myfalse.ToString(false):0
« Last Edit: August 06, 2018, 01:24:03 pm by mobilevil »

wp

  • Hero Member
  • *****
  • Posts: 11917
Re: boolean.ToString()
« Reply #3 on: August 06, 2018, 02:01:41 pm »
from syshelph.inc:
Code: Pascal  [Select][+][-]
  1. type
  2.     TUseBoolStrs = (False, True);
  3.  
  4.     TBooleanHelper = Type Helper for Boolean
  5.     public
  6.       ...
  7.       Function ToString(UseBoolStrs: TUseBoolStrs = TUseBoolStrs.False): string; overload; inline;
  8.     end;
What an insane idea to reassign the boolean values false and true to an enumeration TUseBoolStr = (false, true). Thaddy, you should fire this guy!  ;)

@mobilevil: I normally don't use the helpers at all, only use the plain old classical functions. boolvalue.ToString(UseBoolStrs) does the same as the
Code: Pascal  [Select][+][-]
  1. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
but with an ordinary boolean parameter for UseBoolStrs.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: boolean.ToString()
« Reply #4 on: August 06, 2018, 06:49:25 pm »
And there is also WriteStr() to get 'TRUE' or 'FALSE' into a string.
It's a pity Format() doesn't support booleans.
If you use Lazarus, then you can use the DbgS() function in LCLProc unit.

Bart

Abelisto

  • Jr. Member
  • **
  • Posts: 91
Re: boolean.ToString()
« Reply #5 on: August 06, 2018, 08:35:31 pm »
Old joke:

Junior:
Code: Pascal  [Select][+][-]
  1. if BoolValue then ...

Senior:
Code: Pascal  [Select][+][-]
  1. if BoolValue = True then ...

Guru:
Code: Pascal  [Select][+][-]
  1. if BoolValue.ToString().Length() < 5 then ...
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: boolean.ToString()
« Reply #6 on: August 06, 2018, 09:01:03 pm »
Old joke:

Guru:
Code: Pascal  [Select][+][-]
  1. if BoolValue.ToString().Length() < 5 then ...

Enterprise:
Code: Pascal  [Select][+][-]
  1. if BoolValue.ToString().CompareText('TRUE') = 0 then ...

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14375
  • Sensorship about opinions does not belong here.
Re: boolean.ToString()
« Reply #7 on: August 06, 2018, 09:43:33 pm »
Global ruler:
Code: Pascal  [Select][+][-]
  1.  if Abs(BoolValue.ToInteger) > 0 then
// takes care of all boolean types.
« Last Edit: August 06, 2018, 09:58:34 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

mobilevil

  • Jr. Member
  • **
  • Posts: 69
    • http://www.kachun.com
Re: boolean.ToString()
« Reply #8 on: August 07, 2018, 03:28:06 am »

What an insane idea to reassign the boolean values false and true to an enumeration TUseBoolStr = (false, true). Thaddy, you should fire this guy!  ;)

@mobilevil: I normally don't use the helpers at all, only use the plain old classical functions. boolvalue.ToString(UseBoolStrs) does the same as the
Code: Pascal  [Select][+][-]
  1. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;
but with an ordinary boolean parameter for UseBoolStrs.
too bad we have no control over that guy, seems it's a delphi design.
and yes I have been using BoolToStr. it's just I have been thinking about the pros and cons of primitive types as classes and suddenly found out pascal has it now.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: boolean.ToString()
« Reply #9 on: August 07, 2018, 08:16:50 am »
Maybe BitHelpers can be of some help?
https://forum.lazarus.freepascal.org/index.php/topic,41672.msg289570.html#msg289570

Code: [Select]
  uses
    bithelpers;
  ...
  procedure TForm1.BooleanBitTestBtnClick(Sender: TObject);
  var
    MyBool: boolean;
  begin
    MyBool := true;
    Memo1.Append(MyBool.ToOneZeroString);
    Memo1.Append(MyBool.ToOnOffString); // default is scfUnchangedCase and can be ommited
    Memo1.Append(MyBool.ToOnOffString(scfLowerCase));
    Memo1.Append(MyBool.ToTrueFalseString(scfUpperCase));
    Memo1.Append(MyBool.ToString('OnState', 'OffState')); // true/false custom strings
    Memo1.Append(MyBool.ToString('Укључено', 'Искључено', scfUpperCase)); // when case and unicode matter
  end;
Code: [Select]
  1
  On
  on
  TRUE
  OnState
  УКЉУЧЕНО
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

mobilevil

  • Jr. Member
  • **
  • Posts: 69
    • http://www.kachun.com

 

TinyPortal © 2005-2018