Recent

Author Topic: Boolean to string problem  (Read 817 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Boolean to string problem
« on: December 07, 2020, 12:19:42 am »
 I get the following error trying to do. I wabt to convert Boolean to a string 'True' or 'False'  I could write my own I guess,

unit1.pas(1818,53) Error: Incompatible type for arg no. 2: Got "Boolean", expected "TUseBoolStrs"

Edit1.Text := TBooleanHelper.ToString( OK,False );
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Boolean to string problem
« Reply #1 on: December 07, 2020, 12:26:38 am »
Hi!

Make it simple. In sysutils you find:
Code: Pascal  [Select][+][-]
  1.  
  2. function BoolToStr(B: Boolean;UseBoolStrs:Boolean=False): string;

If you want to do it with your own style use:

Code: Pascal  [Select][+][-]
  1. function BoolToStr(B: Boolean;const TrueS,FalseS:string): string;

Winni

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: Boolean to string problem
« Reply #2 on: December 07, 2020, 12:51:57 am »
[…]I wabt to convert Boolean to a string 'True' or 'False' […]
EP defines that all capabilities write/writeLn have are available with writeStr, too. In your case this means:
Code: Pascal  [Select][+][-]
  1. writeStr(edit1.text, false);
Note, write/writeLn/writeStr’s behavior to produce “true” or “false” respectively is defined as per the standard, but the casing – lower case, upper case – is “implementation-defined”. With FPC, the casing varies among different compiler modes.
Yours Sincerely
Kai Burghardt

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Boolean to string problem
« Reply #3 on: December 07, 2020, 01:06:33 am »
unit1.pas(1818,53) Error: Incompatible type for arg no. 2: Got "Boolean", expected "TUseBoolStrs"

Edit1.Text := TBooleanHelper.ToString( OK,False );

Because of how it's declared, that should be:

Code: Pascal  [Select][+][-]
  1. Edit1.Text := TBooleanHelper.ToString( OK, TUseBoolStrs.False );

otherwise the type of False will be a normal boolean, which doesn't coincide with the signature of the function. Note that you could also (and IMHO more easily) do:

Code: Pascal  [Select][+][-]
  1. Edit1.Text := OK.ToString(TUseBoolStrs.False );

But that's trivial nitpicking and unrelated to your question ... :-[

ETA: BTW, using TUseBoolStrs.False will not produce the strings "False" or "True", but "0" or "1"; if you want the "long" strings you should use:
Code: Pascal  [Select][+][-]
  1. Edit1.Text := OK.ToString(TUseBoolStrs.True);
« Last Edit: December 07, 2020, 01:12:03 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018